English 中文(简体)
动态关键词 - 帮助理解为什么要采用的方法
原标题:RuntimeBinderException - C# .NET 4 Dynamic Keyword - Help Me Understand Why Method Isn t Matching

I ve为HttpModule建立了一个通用的汇合系统,允许吉大港山区长老会的督察。 这里指的是法典的基本规定——这应当足以使我感到:

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的电话中——所提供的工厂名称的所有事例都必须执行IHttp HeaderInspectingAuthenticator Factor<以及(作为帝国的光谱)。 因此,它们都将有一套构造方法,在这种方法中,所供应的T类实际上是采用GetInspector(GetInspector)方法的类别。 例如,GetInspector将被邀请参加基本认证考试——因此,这将成为基本调试师。

然而,对Construct的呼吁没有成功。 该公司认为这种方法是正确的,但显然,参数类型是NOT,根据RuntimeBinderException的推举进行配对。 看来,根据“呼吁”组织。 目标是动态代理预期的类型是Http HeaderInspectingAuthenticatorConfigurationElement——这一Im的过继是源于该基础的基本调子。

因此,情况如何? 我在这里不作什么? 我尝试过过过(这既是动态的)或((Http HeaderInspectingAuthenticatorConfigurationElement)this),但两者都遇到了同样的问题。

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:UsersebrownDocumentsVisual Studio 2010ProjectsUtilitysourceAuthentication LibraryConfigurationHttpHeaderInspectingAuthenticatorConfigurationElement.cs:line 79
  at Redcated.Authentication.Configuration.HttpHeaderInspectingAuthenticatorConfigurationElementCollection.<GetInspectors>b__0(HttpHeaderInspectingAuthenticatorConfigurationElement i) in D:UsersebrownDocumentsVisual Studio 2010ProjectsUtilitysourceAuthentication LibraryConfigurationHttpHeaderInspectingAuthenticatorConfigurationElementCollection.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:UsersebrownDocumentsVisual Studio 2010ProjectsUtilitysourceAuthentication LibraryHttpHeaderInspectingAuthenticationModule.cs:line 49
  at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
  at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

页: 1 我在此问题上进行了努力,改变了接口的结构:

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);
}

因此,我假定,我在这里的理解必须失败......希望有人能够澄清一点。

感谢!

问题回答

我认为,你需要将<条码>这一条码>作为动态。 例如 FactoryInstance.Construct(动力学)this; 我认为,问题在于,动态的援引使用将时间信息汇编成临时代表(Target)的签字不成事实。

由于你执行动态呼吁属于基类,因此它是用于签字的,但由于你逾期签字是你可以使用的子类。 Http HeaderInspectingAuthenticatorConfigurationElement ->BasicAuthentication Header InspectorConfiguration Element, 引证说,你正在向德国航天中心说,论点类型是暂时确定的。

你们说:

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

是否工作?





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签