4

I have a stored procedure which accepts the table name, then it reads the table structure and returns me the table structure in the form of a class definition in a string.

E.g.:

string myString = 
 "
   public class TableName
   { 
     public int Column1 { get; set; } 
   }
 "

Is it possible a create a Class/Type from the string containing the class definition ? For eg:-

Type type = GenerateType(myString);

I have to pass this type variable to my further piece of code so please help me to create class/type from the string containing the class definition.

8
  • 3
    Try Roslyn. But what do you plan to do with instances of a class whose type is unknown at compile time? Commented Jan 2, 2018 at 14:08
  • Are you creating some type of ORM? Commented Jan 2, 2018 at 14:11
  • 2
    I think you are actually looking for serialization. There are many ways to achieve this, it depends on your needs and the complexity of your classes. Usually, serializing to/from Json is sufficient for many cases. Commented Jan 2, 2018 at 14:12
  • Create new type at runtime is very complex do you really need that? I suggest you to find alternatives maybe dynamic or ExpandoObject can help? If you find you have to create a new type give TypeBuilder a look. Commented Jan 2, 2018 at 14:43
  • 1
    This is going to sound like I'm being facetious, but I'm not. Execute the stored procedure, copy the class definition it generates, and then paste that into a class file in your project. Then you will have achieved what you asked in your question - you will have created a type to hold the output of the stored procedure. Commented Jan 2, 2018 at 16:50

1 Answer 1

9

You can use the CSharpCodeProvider to compile your result at runtime and then use the Activator - Class to create an object from your generated code.

// compile your piece of code to dll file
Microsoft.CSharp.CSharpCodeProvider cSharpCodeProvider = new Microsoft.CSharp.CSharpCodeProvider();
System.CodeDom.Compiler.CompilerParameters compilerParameters = new System.CodeDom.Compiler.CompilerParameters();
compilerParameters.GenerateInMemory = true;
compilerParameters.GenerateExecutable = false;
System.CodeDom.Compiler.CompilerResults cResult = cSharpCodeProvider.CompileAssemblyFromSource(compilerParameters, "using System; namespace Tables { 'put here your class definition' }");

// then load your dll file, get type and object from class
Assembly assembly = cResult.CompiledAssembly;
Type myTableType = assembly.GetType("Tables.Tablename");
var finalResult = Activator.CreateInstance(myTableType);
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much. This works just perfectly. Saved my time
Getting an issue in this line 'Assembly assembly = cResult.CompiledAssembly'. This works if I am in the executing assembly (For eg:- Main method is in the same assembly) but in my scenario this code is inside a DLL and I am referring this DLL in my startUp project. In this case I am getting exception 'Cannot load file or assembly'
Check your cResult.Errors => i suggest your code doesn't compile
Yes, There was an error in cResult. Fixed and working now. Many thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.