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.

Prasob
  • 1,963
  • 2
  • 11
  • 20
  • I think you should make your question clearer. What are you trying to do **exactly**? – gdoron Jun 30 '17 at 08:53
  • wow. now that's what i call new. – Nikhil Agrawal Jun 30 '17 at 08: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. – Lasse V. Karlsen Jun 30 '17 at 08:58
  • It this helpful : https://stackoverflow.com/questions/929349/is-there-a-way-to-build-a-new-type-during-runtime ? – Zein Makki Jun 30 '17 at 08: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. – Lasse V. Karlsen Jun 30 '17 at 09:01
  • https://learn.microsoft.com/en-us/dotnet/api/system.reflection.emit.assemblybuilder?view=netframework-4.7 here is a reasonably well explained example. – Headhunter Xamd Jun 30 '17 at 09:12

1 Answers1

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

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142