1
string myClass =" public class AcademicTestQuestion { public int Id { get; set; } public string Content { get; set; } public int ContentType { get; set; } public string Descrtionption { get; set; } public bool IsMandatory { get; set; } public bool IsMultiChoice { get; set; } public DateTime InsertDate { get; set; } public DateTime? UpdateDate { get; set; } public bool IsActive { get; set; } public int AcademicTestPageId { get; set; } } ";

for readability formatted below

 string myClass ="
    public class AcademicTestQuestion
    {
        public int Id { get; set; }
        public string Content { get; set; }
        public int ContentType { get; set; }
        public string Descrtionption { get; set; }
        public bool IsMandatory { get; set; }
        public bool IsMultiChoice { get; set; }
        public DateTime InsertDate { get; set; }
        public DateTime? UpdateDate { get; set; }
        public bool IsActive { get; set; }
        public int AcademicTestPageId { get; set; }
    }
";

How can i create a instance of above string representation of class at run time and use?

I'm trying to generating the table structure in to classes at run time.

6
  • I think you should make your question clearer. What are you trying to do exactly?
    – gdoron
    Commented Jun 30, 2017 at 8:53
  • wow. now that's what i call new. Commented Jun 30, 2017 at 8:53
  • @Recks No, the question was correct to begin with. He wants to compile a class declared in a string. Don't mangle the question. There are some minor syntax problems though, such as missing @ in front of the string, but that does not invalidate the question. Commented Jun 30, 2017 at 8:58
  • It this helpful : stackoverflow.com/questions/929349/… ?
    – Zein Makki
    Commented Jun 30, 2017 at 8:58
  • 1
    I think you need to explain what kind of problem you're trying to solve here. Even if you manage to compile, at runtime, this class, it will still not be available to your program other than as pure object instances. You can use reflection (or dynamic) to access properties on it, but you can't elsewhere declare a variable of type AcademicTestQuestion. Additionally, to actually compile this takes a bit of work so I would urge you to explain more so that we're sure you really need to bark up this tree. Commented Jun 30, 2017 at 9:01

1 Answer 1

2

You cannot do that at runtime unless you have the C# SDK installed.

Which is kind of tricky, because you, as a developer, will generally have the C# SDK installed, so to you it will appear to work, but if you deploy your application to users, the users will generally only have the DotNet runtime environment installed, but not the SDK, so it will not work for them.

If you insist on doing this, begin reading here:

MSDN - System.Runtime.CompilerServices Namespace

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.