0

I'm working on building generic API's where we need to have class objects as configurable so that any changes doesnt require actual code deployment to servers.

for instance, I need class object as below:

Public class Person{
public string  Name{get;set;}
public string  Id{get;set;}
}

I wan to define this structure in Database in string and while application running i will read the above definition from DB and built it and create an instance of it and assign values to the object.

is this possible?? I have been reading about Reflector but what i understand is we can create an instance of existing class dynamically but I need definition itself to built at run time.

what we are trying to achieve: we are building an API where consumers can pass their complex data objects to our system and we perform some validation on their data objects using LINQ Lambda expression and DynamicInvoke for that I need to have c# type object.I'm trying to avoid our application deployment if consumer want to do changes in the input object.

user3212507
  • 173
  • 2
  • 3
  • 11
  • 1
    You're missing a key component in your strategy...your code has to be compiled. At what step do you plan to do that? – Randy Slavey Jan 25 '18 at 02:58
  • 1
    Reflection is to see the current class structure at runtime (so it's kind of read-only). What you need is a runtime compilation. Yes, this is possible, and relatively easy with VS2017, but I'm not sure if your design is a good idea. – Racil Hilan Jan 25 '18 at 03:00
  • 1
    If I needed objects with configurable properties I would probably just go for a dictionary used as a property bag, rather than dealing with the complexity of dynamically compiled code. What benefit does a dynamically compiled type get you? – Mike Zboray Jan 25 '18 at 03:02
  • @RacilHilan I see. I'm still at POC stage of this system. Could you help me with some example?? – user3212507 Jan 25 '18 at 03:13
  • @mikez Mike, i edited the post. Sorry for not mentioning earlier. – user3212507 Jan 25 '18 at 03:14
  • @RandySlavey at Runtime. I came across CSharpCodeProvider which made me think that above is possible. – user3212507 Jan 25 '18 at 03:19
  • 1
    You can see [this](https://stackoverflow.com/questions/826398/is-it-possible-to-dynamically-compile-and-execute-c-sharp-code-fragments) and [this](https://www.codeproject.com/Tips/715891/Compiling-Csharp-Code-at-Runtime). Again, I really don't think it is a good idea. – Racil Hilan Jan 25 '18 at 03:24
  • As mike said, dictionary should be a proper way to do that. Dictionary will eliminate the need of both reflection and dynamic compilation, which are not quite useful in your situation, as you might have noticed. – Diligent Key Presser Jan 25 '18 at 03:28
  • @RacilHilan thanks Racil and understood. BTW, why do you think it is not a good idea?? just want to correct my approach if i'm really wrong. – user3212507 Jan 25 '18 at 03:33
  • In general the question is somewhat similar to "I am going to kill a mosquito with my hand grenade. How do i pull the pin properly?" – Diligent Key Presser Jan 25 '18 at 03:42
  • 2
    It's very hard to list all the points in a comment, but think about it for a second. How is the rest of your code going to interact with this code that is compiled at runtime? You'll need to use reflection. So you're trying to solve an issue, but you'll end up making it a mountain. Believe me, it will be a nightmare. – Racil Hilan Jan 25 '18 at 03:47
  • @DiligentKeyPresser yeah, I was not going for Dictionary as I was not so sure how much clients likes that idea so I started exploring other options but I understand your point. – user3212507 Jan 25 '18 at 04:01
  • @RacilHilan got it. Thank you for the feedback. – user3212507 Jan 25 '18 at 04:02

0 Answers0