Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create type that implements internal interface

Tags:

c#

.net

Suppose I have assembly that declares internal interface IInternalInterface. I have no access to code of this assembly and I can't change it. How can I create my own implementation of IInternalInterface?

Why I need this: the assembly contains the class with list of IInternalInterface implementers and my goal is to add my own implementation there.

like image 635
Denis Palnitsky Avatar asked Dec 22 '11 15:12

Denis Palnitsky


People also ask

Can interface be internal?

The internal interface is properties and methods that can be accessed only from other methods of the object, they are also called "private" (there are other terms, we will meet them further). The external interface is the properties and methods available outside the object, they are called "public".

Can we have internal interface C#?

Access modifiers such as “public” and “internal” are not allowed for interface members. That's because the access modifier for the interface itself determines the access level for all members defined in the interface.

Can abstract class implement interface in C#?

The short answer: An abstract class allows you to create functionality that subclasses can implement or override. An interface only allows you to define functionality, not implement it. And whereas a class can extend only one abstract class, it can take advantage of multiple interfaces.

What is interface type in C#?

Interfaces define properties, methods, and events, which are the members of the interface. Interfaces contain only the declaration of the members. Some of the interface types in C# include. IEnumerable − Base interface for all generic collections. IList − A generic interface implemented by the arrays and the list type.


1 Answers

It is possible using remoting proxy.
Note that my answer is just a quick sketch and might need to be improved further.

internal interface IInternalInterface {
    void SayHello();
}

// --------------------------------------------------------------
// in another assembly
public class ImplementationProxy : RealProxy, IRemotingTypeInfo {
    private readonly MethodInfo method;

    public ImplementationProxy(MethodInfo method) 
        : base(typeof(ContextBoundObject))
    {
        this.method = method;
    }

    public override IMessage Invoke(IMessage msg) {
        if (!(msg is IMethodCallMessage))
            throw new NotSupportedException();

        var call = (IMethodCallMessage)msg;
        if (call.MethodBase != this.method)
            throw new NotSupportedException();

        Console.WriteLine("Hi from internals!");
        return new ReturnMessage(null, null, 0, call.LogicalCallContext, call);
    }

    public bool CanCastTo(Type fromType, object o)
    {
        return fromType == method.DeclaringType;
    }

    public string TypeName
    {
        get { return this.GetType().Name; }
        set { }
    }
}    
like image 197
Andrey Shchekin Avatar answered Oct 19 '22 05:10

Andrey Shchekin