c# - How to initiate T type object of Generic class -


this question has answer here:

i have generic proxy class contains t type object class. wants create object of t.

class proxy<t>: iclient {     t _classobj;      public proxy()      {         this._classobj = //create new instance of type t          } } 

if t class , guarantees has new() operator:

class proxy<t> : iclient t : class, new() {     t _classobj;     public proxy() {         this._classobj = new t();     } } 

otherwise, or if t struct, can do:

class proxy<t>: iclient t : struct {     t _classobj;     public proxy() {         this._classobj = default(t); // null reference-types e.g. classes     } } 

update:

for call method on t there different situations. but, according question , comments, assume t class , has new() operator. also, implements igetdataimplementer has method named getdata. can:

interface igetdataimplementer{     object getdata(); }  class proxy<t> : iclient t : class, igetdataimplementer, new() {     t _classobj;     public proxy() {         this._classobj = new t();         var data = this._classobj.getdata();     } } 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -