过滤结果是使用字节而不是使用反射的整数值进行过滤

Filter result is filtering with a byte and not with an integer value using reflection

我正在使用反射将我的 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"));
        }

但在行中: dropDown.DataSource = typeMethod.Invoke(clase, new object[] { filter }); 给我错误:

Unable to cast object of type 'System.Int32' to type 'System.Byte'.

虽然从 int 到 byte 的隐式转换在常规情况下有效,但不能通过装箱 int(int 作为对象类型)执行。在此处查看类似问题:trying-to-cast-a-boxed-int-to-byte.

由于该方法需要一个字节,但您传递了一个整数,因此您需要在调用该方法之前明确地将其转换为字节:

dropDown.DataSource = typeMethod.Invoke(clase, new object[] { (byte)filter });