Marcel Gangwisch
Marcel Gangwisch

Reputation: 9026

How to assign a generic typed object to a list of generic objects

How can I assign a generic object with type T to a list of generic objects also with type T of another class?

I tried the following:

_openCLHandler.GetMemObjs()[i] = Cl.CreateBuffer<T>(_context, MemFlags.ReadWrite, _size, out _);

The _openCLHandler is a generic class with type T holding a list of objects with IMem. With the CreateBuffer command I want to assign an IMem object.

Is it possible to forward the type from one object to the other?

Upvotes: 0

Views: 58

Answers (1)

PinBack
PinBack

Reputation: 2564

I'm not quite sure I understood the question correctly.
But if you will create an object on the basis of an another object then you have to pass the source object to the other methode.

Maybe this will help

//Your class
public class Foo
{
    public string Value { get; set; }
}

//Your method
private T CreateItem<T>(List<T> poList, string psValue) where T : class, new()
{
    T loNew = new T();

    if (loNew is Foo)
        (loNew as Foo).Value = psValue;

    return loNew;
}

Then you can use these method:

List<Foo> loList = new List<Foo>();
loList.Add(this.CreateItem(loList, "1"));

Upvotes: 1

Related Questions