0

As a result of some service action I'm getting in the result property string with value "MyNamespace.Models.Car"

How can I dynamically create the object (empty) from this string value? Or how else can I dynamically recognize clr type from this string?

var result = service.getSomeData(req);
result.CustomVal holds the value of "MyNamespace.Models.Car";

Note that I cannot change the service.

user1765862
  • 13,635
  • 28
  • 115
  • 220
  • Do you have `Car` class created in `MyNamespace.Models` namespace? – Chetan Oct 16 '18 at 13:06
  • yes I do have Car in the Models namespace – user1765862 Oct 16 '18 at 13:07
  • Take a look at this: https://learn.microsoft.com/en-us/dotnet/api/system.activator.createinstance – user11909 Oct 16 '18 at 13:07
  • Please read [ask] and show what you have tried. – CodeCaster Oct 16 '18 at 13:07
  • If you are dealing with weakly typed services, there is a helper class for you: ExpandoObject. At it's core it is little more then some Syntax Sugar for using a Dictionary with some Change Notificaiton. But it certainly helps when you have to deal with you average, weakly typed webservice. – Christopher Oct 16 '18 at 13:09

1 Answers1

0

With reflection you can find all types in assembly, then use Activator.CreateInstance Documenation

dlxeon
  • 1,932
  • 1
  • 11
  • 14