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.
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".
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.
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.
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.
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 { }
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With