繁体   English   中英

具有多个参数类型的反射

[英]Reflection with multiple parameter types

我有一个数据库表,其中包含两个文本字段:methodname和methodparameters。 表中的值存储在字典中。

每个methodname值对应于ac#class中的图像过滤器方法,每个方法参数是以逗号分隔的数值列表。

我想使用反射来调用methodname及其相应的方法参数列表。

以下是图像过滤器类的一部分:

namespace ImageFilters
{
  public class Filters
  {

    private static Bitmap mBMP;

    public Bitmap BMP { 
        get
        {
            return mBMP;
        }
        set
        {
            mBMP = value;
        }
    }

    public static void FilterColors(string[] paramlist)
    {

        mBMP = FilterColors(mBMP, 
                            Convert.ToInt16(paramlist[0].ToString()),
                            Convert.ToInt16(paramlist[1].ToString()),
                            Convert.ToInt16(paramlist[2].ToString()),
                            Convert.ToInt16(paramlist[3].ToString()),
                            Convert.ToInt16(paramlist[4].ToString()),
                            Convert.ToInt16(paramlist[5].ToString())
                            );

    }

    public static Bitmap FilterColors(Bitmap bmp, int RedFrom,int RedTo, 
                       int GreenFrom, int GreenTo, int BlueFrom, int BlueTo,
                       byte RedFill = 255, byte GreenFill = 255, 
                       byte BlueFill = 255, bool FillOutside = true)
    {
        AForge.Imaging.Filters.ColorFiltering f = new AForge.Imaging.Filters.ColorFiltering();
        f.FillOutsideRange = FillOutside;
        f.FillColor = new AForge.Imaging.RGB(RedFill, GreenFill, BlueFill);
        f.Red = new AForge.IntRange(RedFrom, RedTo);
        f.Green = new AForge.IntRange(GreenFrom, GreenTo);
        f.Blue = new AForge.IntRange(BlueFrom, BlueTo);
        return f.Apply(bmp);
    }

这是我使用的代码使用Reflection:

    private static void ApplyFilters(ref Bitmap bmp, 
                      dictionaries.FilterFields pFilters)
    {

        for(int i = 0; i < pFilters.Detail.Length; i++)
        {
            Type t = typeof(ImageFilters.Filters);
            MethodInfo mi = t.GetMethod(pFilters.Detail[i].MethodName);
            ImageFilters.Filters f = new ImageFilters.Filters();
            f.BMP = bmp;

            string[] parameters = pFilters.Detail[i].MethodParameters.Split(',');
            mi.Invoke(f, parameters);
        }
    }

每个图像都不使用过滤器处理,并使用两组不同的过滤器(来自数据库)。 以下循环处理过滤器:

        foreach (KeyValuePair<string, dictionaries.FilterFields> item 
                 in dictionaries.Filters)
        {
            bmp = OriginalBMP;

            ApplyFilters(ref bmp, item.Value);

        }

我的问题是,当它在循环中遇到ApplyFilters时,它会给我以下错误:

“找不到方法:'Void ImageFilters.Filters.set_BMP(System.Drawing.Bitmap)'。它甚至不允许我进入ApplyFilters方法。

我绝对没有在我的数据库表中有一个名为“set_BMP”的方法。

有任何想法吗?

您得到的错误是JIT错误。 在运行时,您尝试调用ApplyFilters 然后,运行时尝试将ApplyFilters方法从MSIL编译为机器代码。 在那个时间点,它看到你在Filters类上使用了一个名为BMP的属性,但它找不到它(或者找不到setter)。 因此它无法编译该方法而无法调用它,这就是您的断点未被命中的原因。

看来BMP属性(或其setter)在运行时不存在。 这通常是因为在运行时加载了不同版本的程序集 - 您使用具有此属性的一个版本对其进行编译,但在运行它时,引用的程序集不包含该属性。

仔细检查目录中存在的程序集是否是最新的,并且是您期望的正确版本。

您的BMP不是一种方法,它是一种财产。 获取属性然后获取属性的.SetMethod

PropertyInfo pi = type.GetProperty("BMP");
System.Reflection.MethodInfo mi = pi.SetMethod;

string[] parameters = pFilters.Detail[i].MethodParameters.Split(',');
mi.Invoke(f, parameters);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM