2

I would assume it wouldn't be complicated to do the following:

I have a VARCHAR database field that will store a C# property such as "DateTime.Now.Year". I want to pass this value into my ASP.NET application and dynamically return 2017.

How do I read the VARCHAR value as a string and get the C# method to invoke the property?

  • Have you mean storing C# property name as string value in a DB field and invoke it by query to get current year? You can use `Microsoft.CSharp.CSharpCodeProvider` and reflection techniques to execute query results as code (http://stackoverflow.com/questions/4800267/how-to-execute-code-that-is-in-a-string). – Tetsuya Yamamoto Jan 27 '17 at 03:55
  • I've used CS-Script for situations like this: www.csscript.net, which generally works well – Mark Milford Jan 27 '17 at 04:33

1 Answers1

1

You could achieve this using System.Reflection, but you really need to store information about the assembly, class and property/field/method you want to invoke.

for example if you stored:
AssemblyPath: "c:\something\someassembly.dll"
ClassFullPath: "SomeAssembly.SomeNameSpace.SomeClass, SomeClass"
MethodName: "someMethodName"

Then in your code you could attempt to load the assembly and instantiate the class:

var assembly = Assembly.Load(assemblyPath);
var clss = Activator.CreateInstance(ClassFullPath);
var method = clss.GetType().GetMethod(MethodName);
var result = method.Invoke();

Now that is a super simplified example, and there are many things to consider like access to the method / property / field etc. (i.e. private vs public .. and static vs instance)

So if you can really reduce your cases and design a set of flags / options you can store in your table, it will be possible.

meganaut
  • 493
  • 2
  • 10