0

The user will be giving schema information during runtime, and then data which follows the schema.

For instance:

  • asked for class name => Person.
  • asked for number of attributes => 2.
  • asked for attribute with data types => Name, string; Age, integer.

This should create a class in C# as:

class Person
{
    string Name;
    int Age;
}

Then asked for data- Abc,25; Def,30.

So it creates 2 objects of type Person.

The template class would already be there in the project, the attributes and data will be given during runtime, and during the same runtime the objects are created of that class.

I have tried Text Template Transformation Toolkit (T4), and used their design templates, but it takes the schema information during design time, via accessing a xml config file, and creates the classes. Then data is given during runtime.

Is there anyway to give even the schema information during runtime?

T4 also has runtime templates, but from my understanding, that generates text files to be used outside the context of the C# project, whereas design time templates can be used to give C# classes to be used in same project. Correct me if I am wrong.

Sach
  • 10,091
  • 8
  • 47
  • 84
  • 5
    Why you need to create strongly typed classes unknown at compilation time? Dynamic objects prbably will be better aproach. – eocron Jun 20 '19 at 18:36
  • @eocron I haven't read about Dynamic objects, I will read about them. Hopefully they solve the problem. – Sumit Nagpal Jun 20 '19 at 18:44
  • 1
    Dynamic objects may be the solution to the wrong problem. The first part of your question is very clear. Suppose you've stored those inputs - a name and a series of attributes. Each attribute has a name and a type. That's all good. It's not at all clear why you want to create classes out of them or what you want to do with those classes. A class is useless unless other classes work with it. But what classes will you define to work with classes that won't exist until runtime? Even if it's possible to do that with reflection, something other than runtime-generated class likely makes more sense. – Scott Hannen Jun 20 '19 at 18:49
  • 1
    you should look here. it might what are you looking for: [create class dynamically](https://softwareengineering.stackexchange.com/questions/93322/generating-a-class-dynamically-from-types-that-are-fetched-at-runtime) – Power Mouse Jun 20 '19 at 19:18

2 Answers2

1

There is a functionality to create dynamic class objects. Here is the MSDN link for the same. Also. a tutorial to understand the creation of the dynamic classes with required properties during run time

0

as far as i know you cannot instantiate a class like that, a clas must be defined.

but you can create a dynamic class (type safety is gone) and assign the properties then the values.

you can also generate a json and deselialize it to a dynamic calss like in this answer

Bloodday
  • 303
  • 1
  • 15
  • 3
    It is possible. You can interract with compiler (aka JIT) at runtime. But OP is probably just want dynamic class (property bag) as you mentioned. – eocron Jun 20 '19 at 18:38
  • 1
    yes i know that you can create and compile code with roselyn or mono , but instantiate the class that has been created is very hard work (mono and roselyn compile and run code but the scope is outside of your reach), then you will have to use reflection and you will end writing 500 line of code for a problem that can be solved easyly in other ways. – Bloodday Jun 20 '19 at 18:41
  • 3
    This sounds very much like an XY problem. Dynamic could be the answer, or it could be steering someone away from a simpler approach. Whenever we create pretty much anything, we're creating it to be called by something else to accomplish some purpose. I think it's necessary to understand what that purpose is. If it's reasonably possible to start with steering a solution toward type-safe code I'd start with that and only introduce alternatives if they seem absolutely necessary. More often than not it's just a matter of learning to work with the compiler, not against it. – Scott Hannen Jun 20 '19 at 18:57