Question

I want to create a method that invokes a public method, of an instantied class, dynamically (using reflect).

First, I have my class:

namespace NFSe.Classes.Models.Classes.NFSeWeb
{    
    public class Service
    {
        public string IdService { get; set; }
        public string Name {get; set; }

        public getKey()
        {
          return IdService + Name;      
        } 
    }
}

The method "getKey" will be in a few classes.

Ok till there... but I am creating a function that returns the value from function getKey of an object dynamically instantied.

I have a function that I will pass an Object as parameter:

internal static string getValorDaClasse(object valor)
{
    if (valor.ToString().Contains("NFSe.Classes.Models.Classes"))
    {
        Type myType = Type.GetType(valor.ToString());
        object myObj = Activator.CreateInstance(myType);

        //Invoking a non-static method (How to invoke a non static method??)

        return (string)myType.InvokeMember("getKey", BindingFlags.InvokeMethod, null, myObj, new object[] { valor });
     }
     else
         return valor.ToString();
}

When I try to do that (get the value from the method "getKey"... I receive the following exception: Method 'NFSe.Classes.Models.Classes.NFSeWeb.Service.getKey' not found.

All the best!

Était-ce utile?

La solution 3

The right answer is this:

return (string)myType.InvokeMember("getChave", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance, null, myObj, null);

where "getChave" is the name of the function that I want to call.

Autres conseils

You are calling InvokeMember incorrectly. Notice the last parameter of InvokeMember, You are not passing anything into getKey, so null is appropriate there. Also, the binding flags werent allowing you to get the proper method.

return (string)myType.InvokeMember("getKey", 
                                    BindingFlags.InvokeMethod|
                                         BindingFlags.Public | 
                                         BindingFlags.DeclaredOnly | 
                                         BindingFlags.Instance, 
                                    null, 
                                    myObj,
                                    null);

Your Service method:

namespace NFSe.Classes.Models.Classes.NFSeWeb
{
    public class Service
    {
        public string IdService { get; set; }
        public string Name { get; set; }

        public string getKey()
        {
            return IdService + Name;
        }
    }
}

Your calling method:

static void Main(string[] args)
    {
        var mystring = getValorDaClasse("NFSe.Classes.Models.Classes.NFSeWeb.Service");
    }

    public static string getValorDaClasse(object valor)
    {
        if (valor.ToString().Contains("NFSe.Classes.Models.Classes"))
        {
            Type myType = Type.GetType(valor.ToString());
            object myObj = Activator.CreateInstance(myType);

            //Invoking a non-static method (How to invoke a non static method??)

            return (string)myType.InvokeMember("getKey", BindingFlags.InvokeMethod|BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance, null, myObj, null);
        }
        else
            return valor.ToString();
    }

Create a delegate, refer the method with the delegate and pass it as a parameter..

Let me show you a simple example for using delegates..

namespace DelimiterStage1
{
    public delegate void MyDelegate();
    public partial class Form1 : Form
    {


        public Form1()
        {
            InitializeComponent();
            MyDelegate delg = new MyDelegate(catchup);

            new Form2(delg).Show();
        }

        private void catchup()
        {
            label1.Text = "Gotcha!";
        }
    }
}

Form2 --

namespace DelimiterStage1
{
    public partial class Form2 : Form
    {
        public Form2(MyDelegate delgt)
        {
            InitializeComponent();
            delgate_Form2 = delgt;
        }

        MyDelegate delgate_Form2;

        private void button1_Click(object sender, EventArgs e)
        {
            delgate_Form2();
        }
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top