mercredi 9 septembre 2020

Why is a method of a interface not found by C# dynamic on internal classes?

I tried to use dynamic to access methods of classes that are located in another assembly. These classes are internal and created by builders that return a public interface. For some reason dynamic is not able to invoke the method defined on the interface. I can use "classic" reflection to get the code running but I don't understand why it's not working with dynamic.

I know that dynamic is not working with methods of internal classes but here we have a public interface. So please can someone explain why dynamic throws the RuntimeBinderException in the example below?

    namespace SandboxLib  // located in SandboxLib.dll
    {
        public class InternalBuilder
        {
            public static IInterface Build()
            {
                return new InternalClass();
            }
        }

        public interface IInterface
        {
            void IfMethod();
        }
        
        internal class InternalClass : IInterface
        {
            public void IfMethod() { }
        }
    }

    namespace Sandbox  // located in Sandbox.dll
    {
        public class Program
        {
            static void Main(string[] args)
            {
                var inst = InternalBuilder.Build();
                dynamic dynInst = inst;

                inst.IfMethod();     // ok
                dynInst.IfMethod();  // Unhandled exception. Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'object' does not contain a definition for 'IfMethod'
            }
        }
    }




Aucun commentaire:

Enregistrer un commentaire