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
Post a Comment