简体   繁体   中英

Extension Method for Interface in Internal Class

I am currently trying to write extensions to Dynamic Linq . For that I need to add a method signature to the IEnumerableSignatures interface located in the internal class ExpressionParser .

internal class ExpressionParser
{
    interface IEnumerableSignatures
    {
        [...]
    }
}

While I could add the signature to the code directly, I'd rather define this as an extension method to the interface, to keep the original code clean. Usually, to add the method DefaultIfEmpty , I would do something like this:

internal static class Extension 
{
    static void DefaultIfEmpty(this ExpressionParser.IEnumerableSignatures sig);
}

This gives an access error though, because ExpressionParser is internal. I have already tried with several combinations of access level for the class and method.

Is there a way to add an extension method to such an interface or do I have to mess with the original code?

[edit] Turns out that, even when making the interface itself internal (or public for that matter), it is not recognized by dynamic linq. I still got some not-found exception on runtime. So there's no (obvious) way around editing the codeplex code. [/edit]

You can not do that since IEnumerableSignatures is a private interface (it has no access modifier and the default one is private ) and there is no way to access it from outside of ExpressionParser class.

But if you can change the interface access modifier then there should be no problem if you assign the access modifiers correct.

internal static class Extension
{
    internal static int Foo(this ExpressionParser.IEnumerableSignatures b)
    {
        return b.Ib;
    }
}

internal class ExpressionParser
{
    public int Ia { get; set; }
    internal interface IEnumerableSignatures
    {
        int Ib { get; set; }
    }
}

You can not hide your safe (private/less accessible/inner class) inside your house (outer class) if you give the key to someone.

You can not use a less accessible class in a class with higher accessibility. For example using a private class in a public one. You can not make a public extension method for a class that is internal because you can not use the public extension method outside the assembly because you can not access the internal class from outside of assembly!

Both the interface IEnumerableSignatures and the method DefaultIfEmpty are private . If you make them internal (or public ) it will work.

internal class ExpressionParser
{
   internal interface IEnumerableSignatures { }
}

internal static class Extension
{
    internal static void DefaultIfEmpty(this ExpressionParser.IEnumerableSignatures sig) {
        ...
    }
}

It will of course never work if ExpressionParser and Extension are in different assemblies, because they are both internal .

No access modifier is the same as private, so just add internal to the inter face

internal class ExpressionParser 
{
    internal interface IEnumerableSignatures
    {
    }
}
internal static class Extension
{
    static void DefaultIfEmpty(this ExpressionParser.IEnumerableSignatures sig)
    {

    }
}

And it should work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM