C#-在接口的GetCustomAttribute中找不到自定义属性
||
我正在尝试设置如下的自定义属性:
[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();
当我执行以下操作时,我会在列表中看到它,但是\'is \'比较失败:
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\'.
有任何想法吗?
没有找到相关结果
已邀请:
2 个回复
旦彤
瞥同忙接
一旦我做到了,一切都会正常。