0

I have a Generic Proxy Class contains T type object That is also a class. i wants to create a object of T.

class Proxy<T>: IClient
{
    T _classObj;

    public Proxy() 
    {
        this._classObj = //create new instance of type T     
    }
}
helb
  • 7,609
  • 8
  • 36
  • 58
Rahul Kumar
  • 528
  • 1
  • 3
  • 19

2 Answers2

3

You can either use the default value for that type (which will be null for reference types, or one of these for value types: Default Values Table)

_classObj = default(T);

Or apply the new() generic constraint, that forces the type T to have the default parameterless constructor

class Proxy<T>: IClient where T: new()
{
    T _classObj;

    public Proxy() {
       _classObj = new T();
    }
}
dcastro
  • 66,540
  • 21
  • 145
  • 155
  • how to do this _classObj = new _classObj(); _classobject.GetData(); – Rahul Kumar Mar 06 '14 at 11:37
  • @RahulKumar You can't do `new _classObj()` because `_classObj` is the name of a field, not the name of a type. You have to use `new T()` - that's what you're looking for. – dcastro Mar 06 '14 at 11:38
  • thanks,but then how to call the Method's in T – Rahul Kumar Mar 06 '14 at 11:40
  • @RahulKumar You can't, because you don't know what type `T` is. `T` might be an int, or a string - they don't have any `GetData` method. – dcastro Mar 06 '14 at 11:42
  • @RahulKumar Which types do you expect `T` to be? Is there any restriction? Like, `T` can only be a class that implements `ISomeInterface`? – dcastro Mar 06 '14 at 11:42
  • But My T is Type of Class "my own class that containing some methods" that implements IClient interface. – Rahul Kumar Mar 06 '14 at 11:44
  • @RahulKumar Which class? – dcastro Mar 06 '14 at 11:45
  • @dcastro let me modify the code in question – Rahul Kumar Mar 06 '14 at 11:48
  • @RahulKumar I believe you should post another question. This question about "how to instantiate a generic type T" has already been resolved. – dcastro Mar 06 '14 at 11:48
  • sure asking some other question.... i want to convert classes in Type T except RealClient class – Rahul Kumar Mar 06 '14 at 12:03
  • Thanks, i post the another question http://stackoverflow.com/questions/22224047/i-want-to-convert-all-my-classes-in-generic-classes-then-call-classes-methods please do some modification in question if requires – Rahul Kumar Mar 06 '14 at 12:09
0

If T is a class and it guarantees that it has a new() operator:

class Proxy<T> : IClient where T : class, new() {
    T _classObj;
    public Proxy() {
        this._classObj = new T();
    }
}

otherwise, or if T is a struct, so you can do:

class Proxy<T>: IClient where T : struct {
    T _classObj;
    public Proxy() {
        this._classObj = default(T); // which will be null for reference-types e.g. classes
    }
}

UPDATE:

For call a method on T there is some different situations. But, according to question and comments, I assume that T is a class and it has a new() operator. Also, it implements the IGetDataImplementer which has a method named GetData. So we can:

interface IGetDataImplementer{
    object GetData();
}

class Proxy<T> : IClient where T : class, IGetDataImplementer, new() {
    T _classObj;
    public Proxy() {
        this._classObj = new T();
        var data = this._classObj.GetData();
    }
}
amiry jd
  • 27,021
  • 30
  • 116
  • 215