jeudi 16 juillet 2020

Is there a way to properly parse an enum parameter into a method dynamically invoked

imagine the following problem:

//Assembly 1
namespace R {
    public class Remote
    {
        public enum SomeTypes
        {
            A = 12,
            B = 14,
            C = 16
        }
        
        public void DoSomething(SomeTypes s, int a)
        {
            Console.WriteLine((int)s * a);
        }
    }
}

//Assembly 2

namespace L
{
    public class Local
    {
        public enum SomeTypes
        {
            A = 12,
            B = 14,
            C = 16
        }
        
        public Local()
        {
            assembly = ....;
            
            dynamic instance = assembly.DefinedTypes.First(t => t.GetName() == "R.Remote").GetConstructor(Type.EmptyTypes).Invoke(new object [] {});
            
            instance.DoSomething(SomeTypes.A,3); //this is where it crashes because of an argument mismatch, fair point: One is R.Remote.SomeTypes the other one L.Local.SomeTypes
            instance.DoSomething((int)SomeTypes.A,3); //this should work technically, does not, one is R.Remote.SomeTypes the other one int, despite the fact that they can be converted into each other
            instance.DoSomething((dynamic)SomeTypes.A,3); //just a hacky guess, but this does not work either
        }

    }
}

Has anyone an idea how to call DoSomething(...), without the argument mismatch exception triggering?

Just in case: I can't access the Remote assembly.

Thank you very much in advance :)





Aucun commentaire:

Enregistrer un commentaire