简体   繁体   中英

Using Reflection on a referenced class library

I Have A class Library

    namespace LabelFieldsCalc
 {
public class LabelFields
{
    // public string "--";
    public string BARCODE128(LabelCarton carton)
    {
        return "01" + carton.EanTun
            + "3102" + carton.WeightBarcode.ToString()
            + "13" + carton.ItemProdnDate.ToString()
            + "21" + carton.EstNO.ToString() + carton.StationNum.ToString() + carton.ItemSerial.ToString() + "9";
    }
    public string BARCODE128HR(LabelCarton carton)
    {
        return "(01)" + carton.EanTun
            + "(3102)" + carton.WeightBarcode.ToString()
            + "(13)" + carton.ItemProdnDate.ToString()
            + "(21)" + carton.EstNO.ToString() + carton.StationNum.ToString() + carton.ItemSerial.ToString() + "9";
    }
    public string BARCODE13PRICE(LabelCarton carton)
    {
        var LvTempEan = carton.ProdExtraKey4.Substring(1, 8);
        var LcPrice = (carton.Price1 * 100).ToString(">>>>");
        int moddigit = ModuloDigit(LvTempEan + LcPrice);
        return LvTempEan + LcPrice;
    }

    private int ModuloDigit(string v)
    {
        //int k = v.Length;
        int tot1 = 0;
        int tot2 = 0;
        for (int i = v.Length; i < 0; i -= 2)
        {
            tot1 += Convert.ToInt16(v.Substring(i, 1));
        }
        tot1 *= 3;
        for (int i = v.Length - 1; i < 0; i -= 2)
        {
            tot2 += Convert.ToInt16(v.Substring(i, 1));
        }

        tot1 += tot2;

        int k = (tot1.ToString().Length);
        int m = Convert.ToInt16(tot1.ToString().Substring(k, 1));
        if (m == 0)
        {
            return 0;
        }
        return 10 - m;
    }
}
}

that is referenced in a project

I can get at the methods if I do

        LabelFields lv = new LabelFields();
        string value = lv.BARCODE128HR(LL);

But what I actually need to do is to use reflection so that I can decide which method I to use to calculate value at run time

What I have tried is

        Type type = typeof(LabelFields);
        object obj = Activator.CreateInstance(type);
        string res2 = (string)type.InvokeMember("BARCODE128HR", BindingFlags.InvokeMethod, null, obj, mParam);

this gives me

System.MissingMethodException
  HResult=0x80131512
  Message=Attempted to access a missing member.

What am I missing to get the reflection working

Why do you need reflection in the first place? Once you instantiated your obj type of LabelFields, you can call its methods directly.

   Type type = typeof(LabelFields);
   LabelFields obj = (LabelFields)Activator.CreateInstance(type);
   obj.BARCODE128HR(....);

Edit: Your method name is a parameter coming from outside. I'd suggest using an enumeration and a switch:

public enum MethodNames {
   BARCODE128,
   BARCODE128HR, etc
}

/// later
LabelFields obj = new LabelFields();
string lv = "";
switch (MethodName) {
  case MethodNames.BARCODE128:
       lv = obj.BARCODE128HR(mParam);
       break;
  ///and the rest
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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