c# - 具有多种参数类型的反射

标签 c#

我有一个包含两个文本字段的数据库表:方法名称和方法参数。表中的值存储在字典中。

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

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

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

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);
    }

这是我使用的使用反射的代码:

    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 的属性,但它找不到它(或找不到二传手)。因此它无法编译该方法也无法调用它,这就是您的断点未被击中的原因。

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

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

关于c# - 具有多种参数类型的反射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21101893/

相关文章:

c# - 在字符串数组中搜索

c# - TreeView 上下文菜单在 HierarchicalDataTemplate 中不起作用

c# - 为移动设备 (.NET) 在页面中排序框架( block )

c# - 直接使用 Windows Azure Active Directory 进行身份验证,无需打开 Web 浏览器

c# - 无法加载文件或程序集“System.Web.Helpers,IIS 8 上的错误

c# - 为什么 QueueClient 不是一次性的

c# - 在 C# 中将 Aes.Key 转换为 SecureString

c# - Sqlite3 数据库损坏 - 在 Xamarin 中修复

c# - 填充数组时出现 StackoverflowException

c# - 为什么 ReSharper 建议删除它自己的修复程序?