简体   繁体   English

C# - 从Interface的GetCustomAttribute中找不到自定义属性

[英]C# - Custom Attribute not found from GetCustomAttribute from Interface

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 pass

I have tried to do the following that returns false: 我试图做以下返回false:

Attribute.IsDefined(methodInfo, typeof(AuthorizationAttribute));
attribute.GetType().IsAssignableFrom(typeof(AuthorizationAttribute));

I have also done the following 2 things that returns null: 我还做了以下两件返回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. 编辑:我不确定它是否添加任何含义,但AuthorizationAttribute声明存在于与我的服务项目不同的项目中。 The Service Contract interface exists in the same project as the AuthorizationAttribute. Service Contract接口与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? 有任何想法吗?

The exception contains the answer: 例外包含答案:

Type A originates...at location 'F:\\RestServices\\bin\\Lib.dll'. 类型A发起...位于'F:\\ RestServices \\ bin \\ Lib.dll'。 Type B originates...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' 类型B发起...位于'C:\\ Windows \\ Microsoft.NET \\ Framework64 \\ v4.0.30319 \\ Temporary ASP.NET Files \\ oauth_rest \\ 951069b9 \\ 9f7b77fe \\ assembly \\ dl3 \\ 54c48906 \\ f928a6ad_01facb01 \\ Lib.dll'

The issue is that the Lib.OAuth.AuthorizationAttribute type which attributes your method is found in an assembly that is different than the assembly loaded at runtime when you try to cast. 问题是,在您Lib.OAuth.AuthorizationAttribute ,在程序Lib.OAuth.AuthorizationAttribute找到属于您的方法的Lib.OAuth.AuthorizationAttribute类型,该程序集与运行时加载的程序集不同。

Is it possible that one of your projects is using an old version of Lib.dll? 您的某个项目是否可能使用旧版本的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. 我使用一些示例代码进行反射,以通过Assembly.LoadFile(...)方法加载程序集。 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. 问题是,由于我的程序集未在GAC中注册,因此它正在读取IIS服务器上的本地副本,并且比较失败。

For reference, this was my solution: 作为参考,这是我的解决方案:

Assembly.GetExecutingAssembly();

Once I did that, everything worked. 一旦我这样做,一切都有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM