BartMao
BartMao

Reputation: 681

How to invoke the explicitly interface implement method while the interface is private/internal?

I want to invoke the method ICompiledQuery IProvider.Compile(Expression query) defined in System.Data.Linq.SqlClient.SqlProvider class.

But the IProvider interface is internal, so I can not do the cast. Is there any way to invoke the method?

Upvotes: 0

Views: 83

Answers (1)

Patrick Hofman
Patrick Hofman

Reputation: 157048

You can use reflection to get the method and invoke it.

// Some test stuff, replace this with your own.
Expression e = null;
SqlProvider p = new SqlProvider();

// Get the IProvider interface
var iProvider = typeof(SqlProvider).FindInterfaces((t, o) => t.FullName == "System.Data.Linq.Provider.IProvider", null).FirstOrDefault();

if (iProvider != null)
{
    // Get the Compile method on the interface
    MethodInfo m = iProvider.GetMethod("Compile");

    // Call it!
    var output = m.Invoke(p, new object[] { e });
}

Upvotes: 1

Related Questions