Powertip Of The Day-creating your own types

Source: Internet
Author: User

Original article address:Http://app.en25.com/e/es.aspx? S = 1403 & E = 5072 & elq = a8251b41fe1a4d6297a0aecd8e466373

Original article:

Did you know that you can compile any. net source code on the fly and use this to create your own types? Here is an example between strating how to create a new type from C # code that has both static and dynamic methods:

$ Source = @'
Public class Calculator
{
Public static int add (int A, int B)
{
Return (A + B );
}

Public int multiply (int A, int B)
{
Return (A * B );
}
}
'@
Add-type-typedefinition $ Source
[Calculator]
[Calculator] :: Add (5, 10)
$ Mycalculator = New-Object Calculator
$ Mycalculator . Multiply (3, 12)

 

Translation:

you know you can compile any real-time . net Source Code and use them as your custom types? The following example illustrates how to use C # Create a type with static methods and instance methods:

$ Source = @'
Public class Calculator
{
Public static int add (int A, int B)
{
Return (A + B );
}

Public int multiply (int A, int B)
{
Return (A * B );
}
}
'@
Add-type-typedefinition $ Source
[Calculator]
[Calculator] :: Add (5, 10)
$ Mycalculator = New-Object Calculator
$ Mycalculator . Multiply (3, 12)

 

 

Notes:

Directly inPSCodeCreate a class.

Review. NetStatic Method of the class and call method of the instance method.

I personally prefer to compile the logicDLLThenPS.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.