Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 3-Oct-11 0:30am
v4

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
 
Share this answer
 
v2
Comments
Simon Bang Terkildsen 4-Oct-11 2:45am    
Some good thoughts, my 5.
Sergey Alexandrovich Kryukov 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)
This is one of the really good articles on the subject:
Extend Windows PowerShell With Custom Commands[^]

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 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)
In your case, it will be:
$value = [BasicTestererr].Add(4, 1)


—SA
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900