0

I am trying to use generic list as cast of the object. i have seen related questions and tried but didn't get, what i expected.

In the below code i am setting the value to the main response.

Here is code:

Type type = Type.GetType("className, AssemblyName"); <br/>
var z = typeof(List<>).MakeGenericType(type);


  foreach (var key in abc.Keys)
        {
            var value = abc[key];
            foreach (var property in ((List<z>)output.Response)[0].GetType().GetProperties().Where(p => p.CanRead && p.GetMethod.IsPublic).)
            {
                if(property.Name == ((List<Document>)value)[0].GetType().Name)
                {
                    PropertyInfo propertyInfo = ((List<Policy>)output.Response)[0].GetType().GetProperty(property.Name);
                    propertyInfo.SetValue(((List<Policy>)output.Response)[0], ((List<Document>)value));
                }
            }               
        }

while accessing z in list. i am unable to do that. I have stuck from past few hour.Please help me out. Thanks in Advance!

prasanthi
  • 562
  • 1
  • 9
  • 25
  • 5
    `System.Type` is a run-time representation of a type; the `T` in `Foo` is a compile-time type. You can't use runtime types at compile time. For what you're attempting to do, you need to use refelection. – Kenneth K. Feb 26 '20 at 14:09
  • hi @KennethK.,Thanks for replying for my question.In the above code all list should be generic type. To do that what should i do? – prasanthi Feb 26 '20 at 14:11
  • 1
    you only use `z` for casting.. why not use the `dynamic` keyword for `property` instead of `var`? anyhow - this may help you to better understand how to use reflection for your use-case https://stackoverflow.com/a/4101821/4062197 – ymz Feb 26 '20 at 14:45
  • @KennethK.I used **dynamic z** still it showing same error when using List. – prasanthi Feb 27 '20 at 05:32

0 Answers0