Click here to Skip to main content
12,633,985 members (24,156 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: PowerShell C#3.5
I am use below code. using that i can execute C# code. but I want to execute only C# function. pls any one can help ..........

$source = @"
public class BasicTestererr
{
    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
Add(4, 1)
Posted 3-Oct-11 1:18am
Updated 3-Oct-11 1:30am
Slacker007100.6K
v4
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Strange idea. I don't think your whole idea is really useful, but…

You can create an "assembly host" .NET application which loads your assembly with functions Add, Multiply or any other, using Reflection, finds the method to be called by its full name (for example "BasicTestererr.Add"), its formal parameters and return type, call this method with some actual parameters, return the value or process the error, which would be very likely in this case. This tool could also show Reflection-generated documentation showing to the user what parameters are expected and how to interpret the returned value.

Now, about the PowerShell or any other batch language. You can make this "host" application accepting command-line parameters including assembly to be loaded (could be a name of the file of the main executable module of the assembly), a method to be called and actual method parameters. This approach should be limited in the types of parameters and return types: they can be string types or types convertable from/to string, as command line interface only works with strings. You also need to develop a method of reporting errors, in situation when a file not found, entry point is not found, wrong number of parameter or violation of constraints for parameters values; as they are represented as strings, it's easy to make a mistake.

—SA
  Permalink  
v2
Comments
Simon Bang Terkildsen 4-Oct-11 2:45am
   
Some good thoughts, my 5.
SAKryukov 4-Oct-11 3:00am
   
Thank you, Simon; you're way too nice this time...
--SA
lilan madusanka 12-Oct-11 6:34am
   
No I'm want to doing : using C# code, I am call power shell script. then using power shell script execute C# code. (That mean I want to execute dynamic C# code using power shell)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

This is one of the really good articles on the subject:
Extend Windows PowerShell With Custom Commands[^]

Best regards
Espen Harlinn
  Permalink  
Comments
SAKryukov 3-Oct-11 19:46pm
   
Certainly much better answer than mine... :-) My 5.
--SA
Espen Harlinn 4-Oct-11 4:15am
   
Thank you, Sergey!
Simon Bang Terkildsen 4-Oct-11 2:42am
   
great link, my 5
Espen Harlinn 4-Oct-11 4:15am
   
Thank you, Simon!
lilan madusanka 12-Oct-11 6:34am
   
No I'm want to doing : using C# code, I am call power shell script. then using power shell script execute C# code. (That mean I want to execute dynamic C# code using power shell)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

In your case, it will be:
$value = [BasicTestererr].Add(4, 1)


—SA
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161208.2 | Last Updated 28 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100