加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 具有多个参数类型的反射

发布时间:2020-12-15 22:00:23 所属栏目:百科 来源:网络整理
导读:我有一个数据库表,其中包含两个文本字段:methodname和methodparameters.表中的值存储在字典中. 每个methodname值对应于c#类中的图像过滤器方法,每个方法参数是以逗号分隔的数值列表. 我想使用反射来调用methodname及其相应的方法参数列表. 以下是图像过滤器
我有一个数据库表,其中包含两个文本字段:methodname和methodparameters.表中的值存储在字典中.

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

我想使用反射来调用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)在运行时不存在.这通常是因为在运行时加载了不同版本的程序集 – 您使用具有此属性的一个版本对其进行编译,但在运行它时,引用的程序集不包含该属性.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读