从Interface的GetCustomAttribute中找不到C#

编程入门 行业动态 更新时间:2024-10-11 07:28:03
从Interface的GetCustomAttribute中找不到C# - 自定义属性(C# - Custom Attribute not found from GetCustomAttribute from Interface)

我正在尝试设置如下的自定义属性:

[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 pass

I 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.

更多推荐

本文发布于:2023-07-19 07:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1175432.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中找   Interface   GetCustomAttribute

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!