RuntimeBinderException - C#.NET 4动态关键字 - 帮助我理解为什么方法不匹配

时间:2010-05-24 16:01:37

标签: c# .net dynamic .net-4.0

我为HttpModule构建了一个通用配置系统,允许可插入的HTTP头检查器。作为参考,这里是代码的基本布局 - 这应该足以让我感觉到我正在做的事情:

public interface IHttpHeaderInspectingAuthenticatorFactory<T>
    where T: HttpHeaderInspectingAuthenticatorConfigurationElement
{
    IHttpHeaderInspectingAuthenticator<T> Construct(T config);
}

public class BasicAuthenticationInspectingAuthenticatorFactory :
    IHttpHeaderInspectingAuthenticator<BasicAuthenticationHeaderInspectorConfigurationElement>
{
    public IHttpHeaderInspectingAuthenticator<BasicAuthenticationHeaderInspectorConfigurationElement> Construct(BasicAuthenticationHeaderInspectorConfigurationElement config)
    {
        return new BasicAuthenticationInspectingAuthenticator(config);
    }
}

public class BasicAuthenticationInspectingAuthenticator : HttpHeaderInspectingAuthenticatorBase<BasicAuthenticationHeaderInspectorConfigurationElement>
{
    internal BasicAuthenticationInspectingAuthenticator(BasicAuthenticationHeaderInspectorConfigurationElement config) 
        : base(config) {}

 //snip -- IHttpHeaderInspectingAuthenticator<T> and IHttpHeaderInspectingAuthenticator implementation
}


public class BasicAuthenticationHeaderInspectorConfigurationElement : HttpHeaderInspectingAuthenticatorConfigurationElement
{
 //extra properties
}


public class HttpHeaderInspectingAuthenticatorConfigurationElement : ConfigurationElement
{
 protected override void PostDeserialize()
 {
     base.PostDeserialize();

     //simple verification of info supplied in config
     var t = Type.GetType(Factory);
     if (null == t)
         throw new ConfigurationErrorsException(String.Format("The factory type specified [{0}] cannot be found - check configuration settings", Factory ?? ""));

     if (!typeof(IHttpHeaderInspectingAuthenticatorFactory<>).IsGenericInterfaceAssignableFrom(t))
         throw new ConfigurationErrorsException(String.Format("The factory type specified [{0}] must derive from {1} - check configuration settings", Factory ?? "", typeof(IHttpHeaderInspectingAuthenticatorFactory<>).Name));

     var c = t.GetConstructor(Type.EmptyTypes);
     if (null == c)
         throw new ConfigurationErrorsException(String.Format("The factory type specified [{0}] must have a parameterless constructor - check configuration settings", Factory ?? ""));
 }

 [ConfigurationProperty("factory", IsRequired = true)]
 public string Factory
 {
  get { return (string)this["factory"]; }
  set { this["factory"] = value; }
 }

 //this allows us to use types derived from HttpHeaderInspectingAuthenticatorConfigurationElement
    protected override bool OnDeserializeUnrecognizedAttribute(string name, string value)
    {                    
        ConfigurationProperty property = new ConfigurationProperty(name, typeof(string), value);
        Properties.Add(property);
        base[property] = value;            
        return true;
    }

 public IHttpHeaderInspectingAuthenticator GetInspector()
 {
     dynamic factoryInstance = Activator.CreateInstance(Type.GetType(Factory));
     return factoryInstance.Construct(this);
 }
}

现在问题出现在GetInspector调用中 - 提供的Factory名称的所有实例都必须实现IHttpHeaderInspectingAuthenticatorFactor&lt;&gt; (如PostDeserialize中的规范)。因此,它们都将具有Construct方法,其中提供的类型T实际上将是实现GetInspector()方法的类的实际类型。因此,例如,将在BasicAuthenticationHeaderInspectorConfigurationElement的实例上调用GetInspector - 因此'this'将是BasicAuthenticationHeaderInspectorConfigurationElement的实例。

但是,对Construct的调用失败。它找到方法很好,但显然参数类型不匹配基于抛出的RuntimeBinderException。基于CallSite.Target,似乎动态代理期望的预期类型是HttpHeaderInspectingAuthenticatorConfigurationElement - 我传递的'this'是从该基础派生的BasicAuthenticationHeaderInspectorConfigurationElement。

那是什么给出的?我在这里没有得到什么吗?我尝试传递(这是动态的)或((HttpHeaderInspectingAuthenticatorConfigurationElement)) - 但两者都失败了同样的问题。

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: The best overloaded method match for 'Redcated.Authentication.BasicAuthenticationInspectingAuthenticatorFactory.Construct(Redcated.Authentication.Configuration.BasicAuthenticationHeaderInspectorConfigurationElement)' has some invalid arguments
  at CallSite.Target(Closure , CallSite , Object , HttpHeaderInspectingAuthenticatorConfigurationElement )
  at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
  at Redcated.Authentication.Configuration.HttpHeaderInspectingAuthenticatorConfigurationElement.GetInspector() in D:\Users\ebrown\Documents\Visual Studio 2010\Projects\Utility\source\Authentication Library\Configuration\HttpHeaderInspectingAuthenticatorConfigurationElement.cs:line 79
  at Redcated.Authentication.Configuration.HttpHeaderInspectingAuthenticatorConfigurationElementCollection.<GetInspectors>b__0(HttpHeaderInspectingAuthenticatorConfigurationElement i) in D:\Users\ebrown\Documents\Visual Studio 2010\Projects\Utility\source\Authentication Library\Configuration\HttpHeaderInspectingAuthenticatorConfigurationElementCollection.cs:line 78
  at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
  at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
  at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)
  at Redcated.Authentication.HttpHeaderInspectingAuthenticationModule.AuthenticateRequest(Object sender, EventArgs e) in D:\Users\ebrown\Documents\Visual Studio 2010\Projects\Utility\source\Authentication Library\HttpHeaderInspectingAuthenticationModule.cs:line 49
  at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
  at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

仅供参考 - 我通过改变接口的架构来解决这个问题:

public interface IHttpHeaderInspectingAuthenticatorFactory
{
    IHttpHeaderInspectingAuthenticator Construct(HttpHeaderInspectingAuthenticatorConfigurationElement config);
}

public interface IHttpHeaderInspectingAuthenticatorFactory<T> : IHttpHeaderInspectingAuthenticatorFactory
    where T: HttpHeaderInspectingAuthenticatorConfigurationElement
{
    IHttpHeaderInspectingAuthenticator<T> Construct(T config);
}

public abstract class HttpHeaderInspectingAuthenticatorFactoryBase<T> : IHttpHeaderInspectingAuthenticatorFactory<T>
            where T : HttpHeaderInspectingAuthenticatorConfigurationElement
{
    protected static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    public abstract IHttpHeaderInspectingAuthenticator<T> Construct(T config);

    public IHttpHeaderInspectingAuthenticator Construct(HttpHeaderInspectingAuthenticatorConfigurationElement config)
    {
        return Construct((T)config);
    }
}

 public class BasicAuthenticationInspectingAuthenticatorFactory :
    HttpHeaderInspectingAuthenticatorFactoryBase<BasicAuthenticationHeaderInspectorConfigurationElement>
{
    public override IHttpHeaderInspectingAuthenticator<BasicAuthenticationHeaderInspectorConfigurationElement> Construct(BasicAuthenticationHeaderInspectorConfigurationElement config)
    {
        return new BasicAuthenticationInspectingAuthenticator(config);
    }
}

当然,GetInspector调用变为

public IHttpHeaderInspectingAuthenticator GetInspector()
{
    var factoryInstance = (IHttpHeaderInspectingAuthenticatorFactory)Activator.CreateInstance(Type.GetType(Factory));
    return factoryInstance.Construct(this);
}

所以我认为我的理解必定会有所失误...希望有人可以解释一下。

谢谢!

2 个答案:

答案 0 :(得分:3)

我认为你需要将this视为动态的。例如factoryInstance.Construct((dynamic)this);我认为问题是默认情况下动态调用使用缓存委托(Target)签名的编译时信息。

由于动态调用的实现在基类中,因此它用于签名,但由于您的后期绑定签名是子类,因此无法进行HttpHeaderInspectingAuthenticatorConfigurationElement - &gt; BasicAuthenticationHeaderInspectorConfigurationElement,因此通过将参数强制转换为动态,您告诉DLR参数类型是在运行时确定的。

答案 1 :(得分:-1)

你是说:

 dynamic factoryInstance = Activator.CreateInstance(Type.GetType(Factory));
 dynamic parameter = this;
 return factoryInstance.Construct(parameter); 

没用?