48

At run-time, I don't know what type of variable v1 is. For this reason, I wrote many if else statements:

if (v1 is ShellProperty<int?>)
{
    v2 = (v1 as ShellProperty<int?>).Value;
}
else if (v1 is ShellProperty<uint?>)
{
    v2 = (v1 as ShellProperty<uint?>).Value;
}
else if (v1 is ShellProperty<string>)
{
    v2 = (v1 as ShellProperty<string>).Value;
}
else if (v1 is ShellProperty<object>)
{
    v2 = (v1 as ShellProperty<object>).Value;
}    

The only difference is in ShellProperty<AnyType>.

So instead of writing this with a lot of if else statements, I decided to use reflection to get the property type at run-time:

 Type t1 = v1.GetType().GetProperty("Value").PropertyType;
 dynamic v2 = (v1 as ShellProperty<t1>).Value;

This code gets the PropertyType of v1 and assigns it to the local variable t1, but after that, my compiler says that:

t1 is a variable but is used like a type

So it does not allow me to write t1 inside ShellProperty<>.

Please tell me how to solve this problem and how to get more compact code than what I have. Do I need to create a new class?

4
  • 1
    You can't use generics in that way. Generics are strongly typed at compile time.
    – David L
    Oct 4, 2015 at 15:48
  • So you mean it is not possible Oct 4, 2015 at 15:51
  • not statically like this. but you are already doing dynamic. Oct 4, 2015 at 15:51
  • Not using dyanmic objects. If your value is an object (i.e. inherits from System.Object or better yet some base class that you own), you could do object v2 = v1 as ShellProperty<t1>. (Again, using your own base class--or better yet an interface--would make this more useful.)
    – devstruck
    Oct 4, 2015 at 15:53

2 Answers 2

29

You were very close, you were just missing a call to MakeGenericType.

I believe your code would look like the following:

Type t1 = v1.GetType().GetProperty("Value").PropertyType;
var shellPropertyType = typeof(ShellProperty<>);
var specificShellPropertyType = shellPropertyType.MakeGenericType(t1);
dynamic v2 = specificShellPropertyType.GetProperty("Value").GetValue(v1, null);

Edit: As @PetSerAl pointed out I added some layers of indirection that were unnecessary. Sorry OP, you probably want a one liner like:

dynamic v2 = v1.GetType().GetProperty("Value").GetValue(v1, null);
4
  • 2
    Is that: v1.GetType().GetProperty("Value").GetValue(v1, null) not enough? Oct 4, 2015 at 16:07
  • 1
    @PetSerAl You're absolutely right, I feel very silly. I spent too much time looking at the original code and not enough time thinking about my answer. I will update now.
    – OxCantEven
    Oct 4, 2015 at 16:14
  • 2
    very funny! maybe @PetSerAl is right! But anyway this was for "How to use Local variable as a type?" Oct 5, 2015 at 7:00
  • 1
    I believe this was not for the question "How to use Local variable as a type?" as i get the same error... Jan 4, 2022 at 19:36
17

For generics, you have to create them dynamically.

MethodInfo method = typeof(Sample).GetMethod("GenericMethod");
MethodInfo generic = method.MakeGenericMethod(myType);
generic.Invoke(this, null);

To create a generic object, you can

var type = typeof(ShellProperty<>).MakeGenericType(typeof(SomeObject));
var v2 = Activator.CreateInstance(type);

Please refer to Initializing a Generic variable from a C# Type Variable

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.