Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How best to create and execute a method in a .NET (C#) class dynamically through configuration

Tags:

c#

.net

dynamic

I'm thinking of something along the lines of the "Inline Task" in MsBuild. For reference: http://msdn.microsoft.com/en-us/library/dd722601.aspx

I'd like to find or create a framework which allows me to override a method via configuration. For example if I have a well known base class which has a method Execute(args), how can I supply an overridden method implementation at deployment time, without requiring new code, build, release cycle? I would like to actually plug in the method body into a config file or preferably a database table.

I assume this would be done either with code dom, dynamic language integration, or perhaps something like powershell(?). I'm looking for recommendations or perhaps a library someone has already written.

The application is written in C#. Preferably the extension would also be in C#, but I'm open to other ideas as well.

Update: Technically I don't even have to actually override a method. It would be sufficient to just be able to dynamically execute some external source code, passing in an arg and returning a result.

Update. I ended up writing code to instantiate a PowerShell object and execute a script dynamically to return a value. Here is a snippet of code I used.

    public static Collection<PSObject> ExecuteScript(string code, string variableName, object variableValue)
        {
            PowerShell ps = PowerShell.Create();    

            ps.AddScript(code);

            if (!string.IsNullOrWhiteSpace(variableName))
            {                   
                ps.Runspace.SessionStateProxy.SetVariable(variableName, variableValue);
            }

            var result = ps.Invoke();

            return result;
        }

Then in the calling code, I simply check the first PSObject in the return value, and pull the resulting value from it. It works great. Thanks for all the responses.

like image 691
randbrown Avatar asked Aug 23 '11 14:08

randbrown


People also ask

How do you execute a method in C#?

After creating function, you need to call it in Main() method to execute. In order to call method, you need to create object of containing class, then followed bydot(.) operator you can call the method. If method is static, then there is no need to create object and you can directly call it followed by class name.


1 Answers

Here are two examples of dynamic execution. I have used neither though so I can't comment further.

http://www.codeproject.com/KB/dotnet/evaluator.aspx
http://www.csharpfriends.com/articles/getarticle.aspx?articleid=118

Regarding namespaces, from the second article you can add assemblies through the CompilerParameter class.

// Create the C# compiler
CSharpCodeProvider csCompiler = new CSharpCodeProvider();
ICodeCompiler iCodeCompiler = csCompiler.CreateCompiler();

// input params for the compiler
CompilerParameters compilerParams = new CompilerParameters();
compilerParams.OutputAssembly = "CSharpFriends.dll";
compilerParams.ReferencedAssemblies.Add("system.dll");
like image 189
bic Avatar answered Oct 30 '22 01:10

bic