C# 筛选结果是使用反射使用字节而不是整数值进行筛选
我正在使用反射绑定我的C# 筛选结果是使用反射使用字节而不是整数值进行筛选,c#,reflection,C#,Reflection,我正在使用反射绑定我的TableAdapters结果,生成一个dropdownlist,刚才我正在使用: DropDownBinding.Bind<Cat_AgTableAdapter>(CbxAg, "Cod", "AgenV", Convert.ToInt32(CbxReg.SelectedValue)); public static void Bind<T>(DropDownList dropDown, string textField, string value
TableAdapters
结果,生成一个dropdownlist
,刚才我正在使用:
DropDownBinding.Bind<Cat_AgTableAdapter>(CbxAg, "Cod", "AgenV", Convert.ToInt32(CbxReg.SelectedValue));
public static void Bind<T>(DropDownList dropDown, string textField, string valueField, int filter, string method, bool defaultSelect) where T : new()
{
var type = typeof(T);
var typeMethod = type.GetMethod(method);
var clase = Activator.CreateInstance(type);
dropDown.DataSource = typeMethod.Invoke(clase, new object[] { filter });
dropDown.DataTextField = textField;
dropDown.DataValueField = valueField;
dropDown.DataBind();
if (defaultSelect)
dropDown.Items.Insert(0, new ListItem("-- Select an option--", "-1"));
}
虽然从int到byte的隐式转换在常规情况下有效,但不能通过装箱的int(int作为对象类型)执行。请参见此处的类似问题: 由于该方法需要一个字节,但您传递了一个整数,因此需要在调用该方法之前将其显式转换为字节:
dropDown.DataSource = typeMethod.Invoke(clase, new object[] { (byte)filter });
过滤器参数类型是Int32,我想您的方法需要字节,但不是Int32。。。
dropDown.DataSource = typeMethod.Invoke(clase, new object[] { (byte)filter });