Reflection with several types of parameters

I have a database table containing two text fields: methodname and methodparameters. Values ​​from the table are stored in the dictionary.

Each method name value corresponds to an image filter method in the C # class, and each of the method parameters is a comma-delimited list of numerical values.

I want to use reflection to call the name method with my corresponding list of method parameters.

Here is part of the image filter class:

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

Here is the code I'm using that uses 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);
        }
    }

Each image is processed without filters and with two sets of different filters (from the database). The following loop processes the filters:

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

            ApplyFilters(ref bmp, item.Value);

        }

My problem is that when it gets into ApplyFilters in a loop, it causes the following error:

" : 'Void ImageFilters.Filters.set_BMP (System.Drawing.Bitmap)'. ApplyFilters.

, "set_BMP" .

?

+4
2

, , JIT. ApplyFilters. ApplyFilters MSIL . , BMP Filters, ( ). , .

, BMP ( ) . - , - , , .

, , , , .

+1

BMP , . , .SetMethod .

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

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

All Articles