c# - Create object of parameter type -


hey. possible have method allows user pass in parameter of type , have method instantiate new object of type? this: (i don't know if generics way go, gave shot)

    public void loaddata<t>(t, string id, string value) t : new()     {          this.item.add(new t() { id=id, val = value});      } 

the above doesn't work, idea user passes object type want instantiate , method fill in details based on parameters. pass enum parameter , switch , create new objects based on that, there better way? thanks

the way add interface specifies parameters want set:

public interface isettable {     string id { get; set; }     string val { get; set; } }  public void loaddata<t>(string id, string value) t : isettable, new() {     this.item.add(new t { id = id, val = value }); } 

unfortunately can't test verify @ moment.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -