I would think that this should work…but it doesn’t. I get a MissingMemberException.
class Program
{
static void Main(string[] args)
{
typeof(Class1).InvokeMember("Prop",
System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.SetProperty, null, new Class1(), new object[] { TestEnum.One });
typeof(Class1).InvokeMember("Prop",
System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.SetProperty, null, new Class1(), new object[] { (int)1 });
}
}
public class Class1
{
public TestEnum Prop { get; set; }
}
public enum TestEnum : int
{
One = 1,
Two,
Three
}
This seems to contradict the behavior of every other System.Reflection method…any ideas on how to get the DefaultBinder to correctly identify the method to use? Or another approach?
The first one works. Only the second one doesn’t. And the reason therefore is that there is no property with name
Propand typeintinClass1. If you cast1toTestEnuminstead ofintit will work.This behavior is consistent with other Reflection behavior. If the types of the parameters don’t match, the member is not found.