我正在尝试设置如下的自定义属性:
[AttributeUsageAttribute(AttributeTargets.Method)] public sealed class AuthorizationAttribute : Attribute { public AuthorizationAttribute(bool required) { Required = required; } public bool Required; }在我的服务契约接口中,我有一个这样的方法:
[OperationContract] [Authorization(true)] [WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "randommethod")] ReturnObject RandomMethod();当我做下面的事情时,我在列表中看到它,但是'是'比较失败:
foreach(object attribute in methodInfo.GetCustomAttributes(true)) // Returns all 3 of my attributes. if (attribute is AuthorizationAttribute) //Does not pass我试图做以下返回false:
Attribute.IsDefined(methodInfo, typeof(AuthorizationAttribute)); attribute.GetType().IsAssignableFrom(typeof(AuthorizationAttribute));我也做了以下两件返回null的东西:
AuthorizationAttribute authAttribute = attribute as AuthorizationAttribute; Attribute attribute = Attribute.GetCustomAttribute(methodInfo, typeof(AuthorizationAttribute));我不确定我在这里做错了什么。 它似乎应该工作,但我相信我在某个地方犯了一个简单的错误。 任何见解?
感谢您的帮助。
编辑:我不确定它是否增加了任何含义,但AuthorizationAttribute声明存在于与我的服务项目不同的项目中。 服务合同界面与AuthorizationAttribute存在于同一个项目中。
我试图做一个演员,并得到以下例外:
[A]Lib.OAuth.AuthorizationAttribute cannot be cast to [B]Lib.OAuth.AuthorizationAttribute. Type A originates from 'Lib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' at location 'F:\RestServices\bin\Lib.dll'. Type B originates from 'Lib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\oauth_rest\951069b9 \9f7b77fe\assembly\dl3\54c48906\f928a6ad_01facb01\Lib.dll'.有任何想法吗?
I am attempting to setup a custom attribute like the following:
[AttributeUsageAttribute(AttributeTargets.Method)] public sealed class AuthorizationAttribute : Attribute { public AuthorizationAttribute(bool required) { Required = required; } public bool Required; }In my service contract interface, I have a method like such:
[OperationContract] [Authorization(true)] [WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "randommethod")] ReturnObject RandomMethod();When I do the following I see it in the list, but but the 'is' comparison, fails:
foreach(object attribute in methodInfo.GetCustomAttributes(true)) // Returns all 3 of my attributes. if (attribute is AuthorizationAttribute) //Does not passI have tried to do the following that returns false:
Attribute.IsDefined(methodInfo, typeof(AuthorizationAttribute)); attribute.GetType().IsAssignableFrom(typeof(AuthorizationAttribute));I have also done the following 2 things that returns null:
AuthorizationAttribute authAttribute = attribute as AuthorizationAttribute; Attribute attribute = Attribute.GetCustomAttribute(methodInfo, typeof(AuthorizationAttribute));I am not sure what I am doing wrong here. It seems like it should work, but I am sure I am making a simple mistake somewhere. Any insight?
Thanks for any assistance.
Edit: I am not sure if it adds any meaning, but the AuthorizationAttribute declaration exists in a different project from my services project. The Service Contract interface exists in the same project as the AuthorizationAttribute.
I tried doing a cast and got the following exception:
[A]Lib.OAuth.AuthorizationAttribute cannot be cast to [B]Lib.OAuth.AuthorizationAttribute. Type A originates from 'Lib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' at location 'F:\RestServices\bin\Lib.dll'. Type B originates from 'Lib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\oauth_rest\951069b9 \9f7b77fe\assembly\dl3\54c48906\f928a6ad_01facb01\Lib.dll'.Any ideas?
最满意答案
例外包含答案:
类型A始于...位置'F:\ RestServices \ bin \ Lib.dll'。 类型B源于...位置'C:\ Windows \ Microsoft.NET \ Framework64 \ v4.0.30319 \ Temporary ASP.NET Files \ oauth_rest \ 951069b9 \ 9f7b77fe \ assembly \ dl3 \ 54c48906 \ f928a6ad_01facb01 \ Lib.dll'
问题在于,在您试图进行Lib.OAuth.AuthorizationAttribute时,在与运行时加载的程序集不同的程序集中可以找到属于您的方法的Lib.OAuth.AuthorizationAttribute类型。
您的某个项目是否可能使用旧版本的Lib.dll?
Thanks to Wesley's response, I was able to figure this out. It is more of a 'duh' moment than anything.
I was using some example code for reflection to load an assembly via the Assembly.LoadFile(...) method. The problem is that since my assembly was not registered with the GAC, it was reading the local copy on the IIS server and the comparison failed.
For reference, this was my solution:
Assembly.GetExecutingAssembly();Once I did that, everything worked.
更多推荐
发布评论