Extension Method for Interface in Internal Class

.net c# dynamic-linq extension-methods internal-class

Question

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]

1
1
12/6/2015 1:34:47 PM

Accepted Answer

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!

6
6/25/2015 9:29:01 AM

Popular Answer

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



Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow