-1

Hey I have tried to use a Type as Parameter and using typeof(Class) to get the type.

My problem is that i cant use the Type in a Task like this:

CreditorList = new ObservableCollection<DataModel>();

Is this possible??

Code for Reference:

private void Dg_Loaded(object sender, RoutedEventArgs e)
        {
            CreateList(typeof(CreditorClient));
        }    

async private void CreateList(Type DataModel)
        {
            CreditorList = new ObservableCollection<DataModel>();
        }

Error:

Cannot implicitly convert type '...OberservableCollection' to '...DataModel.CreditorClient'

3
  • 1
    What type is CreditorList ? Commented Sep 8, 2017 at 11:51
  • Why not use ObservableCollection<object>? Commented Sep 8, 2017 at 11:59
  • This is almost never the right thing to do. Even having created the object successfully, you will have trouble actually using the object if you can't statically declare the generic type for variables which will be used to access the object. But, if you insist, this is an all-too-often posted question. One of the earliest duplicates is shown above. Commented Sep 8, 2017 at 20:49

3 Answers 3

0

You can't do this directly you would have to use reflection to create the list it you want to pass it as a Type.

CreditorList = Activator.CreateInstance(typeof(ObservableCollection<>).MakeGenericType(DataModel))

I would recommend you find a design where this is not required though. Reflection is slow and ugly and you might run into problems with it.

Sign up to request clarification or add additional context in comments.

2 Comments

shows same error... just with object instead of OberservableCollection
That is why I asked for the type of CreditorList you have to cast it to that type .. hopefully some interface the ObservableCollection implements that has no generic arguments, otherwise this is not apluicable.
0

Try to use reflection:

private void Dg_Loaded(object sender, RoutedEventArgs e)
{
    CreateList(typeof(CreditorClient));
}    

async private void CreateList(Type DataModel)
{
    Type genericType = typeof(ObservableCollection<>).MakeGenericType(DataModel);
    CreditorList = new ObservableCollection<Activator.CreateInstance(genericType)>();
}

10 Comments

Same Error.....
What type is CreditorList?
Im not Sure.. when i try making a break point on typeof(CreditorList) i gives me the path to the class...
How are you defining CreditorList? It looks like some global variable in you example.
public ObservableCollection<CreditorClient> CreditorList { get; set; }
|
-1

Please try the following code:

private void Dg_Loaded(object sender, RoutedEventArgs e)
        {
            CreateList<CreditorClient>();
        }    

async private void CreateList<TModel>()
        {
            CreditorList = new ObservableCollection<TModel>();
        }

1 Comment

shows same error... just with <TModel> instead of OberservableCollection

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.