dimanche 23 octobre 2016

How to specify an interface as a generic type argument when using reflection in C#?

I am trying to instantiate a type using reflection that takes a contract as a generic type parameter. If it was a generic type method, I could use the .MakeGenricMethod method, and specify the reflected type. However, if the type itself is not generic, how would I go about specifying the interface as the contract?

This is what the code would look like with the assemblies loaded normally:

Ice.Lib.Framework.WCFServiceSupport.CreateImpl<Erp.Proxy.BO.JobEntryImpl>(EpicorSession, Epicor.ServiceModel.Channels.ImplBase<Erp.Contracts.JobEntrySvcContract>.UriPath);

Where I am stuck is at the "Epicor.ServiceModel.Channels.ImplBase" part.

I need to specify that Erp.Contracts.JobEntrySvcContract interface when reflecting or the class won't instantiate properly. I then need to grab that .UriPath property and plug it into my CreateImpl method.

Here's what I've got for that part:

 Type _ImplBase = asmEpicorServiceModel.GetType("Epicor.ServiceModel.Channels.ImplBase");      
            FieldInfo UriPath = _ImplBase.GetField("UriPath", BindingFlags.GetField | BindingFlags.Public | BindingFlags.Static); 

The problem is, _ImplBase is returning null, and I think this is because I didn't specify that Interface as the contract so it failed.

public class ImplBase<TContract> : ImplBase where TContract : class
{
    public static readonly string UriPath;

Eventually, I will need to grab that UriPath static property.

Thank you!





Aucun commentaire:

Enregistrer un commentaire