簡體   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