0

I want to instantiate a type inside parentheses, how do I do that?

        var typeStr = "System.Int32";
        var type = Type.GetType(typeStr);
        
        Console.WriteLine(type);
        object o = Activator.CreateInstance(type);
        
        var i = (type instantiation) o;

That is, get the type inside the brackets at compile time

That's not how it works

        var typeStr = "System.Int32";
        var type = Type.GetType(typeStr);
        
        Console.WriteLine(type);
        object o = Activator.CreateInstance(type);
        
        var i = (type) o;
  • Did you check [this post](https://stackoverflow.com/questions/752/how-to-create-a-new-object-instance-from-a-type?rq=2)? – Mustafa Özçetin Jun 12 '23 at 23:49
  • in var ,The data Type is recognized by the compiler at compile time.you can't to specific at run time – abolfazl sadeghi Jun 12 '23 at 23:52
  • 1
    `var i = (type) o;` doesn't make any sense. What would you do with the type if you only knew about it at runtime? The whole point of static typing of variables is that it's *static* so you know at compile time. – Charlieface Jun 13 '23 at 00:44
  • _"That is, get the type inside the brackets at compile time"_ - if you know at compile time what the type is, why bother with all of this? – Fildor Jun 13 '23 at 06:12

0 Answers0