C # isdefined Issues _c# Tutorials

Source: Internet
Author: User
Tags reflection

Under. NET 4.0 (including, of course, 4.0 previous versions), you can confirm by calling the MethodInfo isdefined () method by using reflection to determine whether a method uses custom attributes. Of course, the isdefined () method is in fact defined in the parent class memberinfo of MethodInfo, but it is only defined as an abstract method, and the real implementation is in the MethodInfo subclass DynamicMethod. The calling method looks like this:

Copy Code code as follows:
Methodinfo.isdefined (typeof (MyAttribute), false)

However, in the actual development, I found that the method has a problem. If you get methodinfo by loading the assembly and then using the reflection method to get the MethodInfo object, even if the method uses a custom attribute, the result returned is false. For example, we define the class of the method that needs to be judged into a separate project and compile it into a separate DLL file, and then use Assembly's LoadFile () method to get the assembly:

Copy Code code as follows:
var assembly = Assembly.loadfile (AssemblyPath);
var types = assembly. Getexportedtypes ();
Types. ToList (). ForEach (
Type =>
{
var flag =
Type. GetMethods (). Where (MethodInfo =>!methodinfo.isabstract). Any (
MethodInfo => methodinfo.isdefined (typeof (MyAttribute), false));
Console.WriteLine ("Flag of isdefined is: {0}", Flag);
}
);

The printed value is false.

Conversely, if the method is not loaded by loading the assembly, but rather the type obtained directly through typeof (), and the next methodinfo.isdefined () method is invoked, the returned result is true as long as it is applied with the specified attribute.

The reason for analysis is approximately the result of different ways of getting type. The Getexportedtype () implementation of the Assembly class is as follows:

Copy Code code as follows:
[SecuritySafeCritical]
public override type[] Getexportedtypes ()
{
type[] o = null;
Getexportedtypes (this. Getnativehandle (), jithelpers.getobjecthandleonstack<type[]> (ref O));
return o;
}

Note that the type[] returned here is in fact passed to the Jithelpers getobjecthandleonstack<type[]> method by reference:

Copy Code code as follows:
[Targetedpatchingoptout ("performance critical to inline across NGen image boundaries"), SecurityCritical]
Internal static objecthandleonstack getobjecthandleonstack<t> (ref T O) where T:class
{
TypedReference reference = __makeref (o);
Return to new Objecthandleonstack (reference. Getpointeronstack ());
}

This converts the type to TypedReference. The key is about here, but I can't find the specific implementation of typeof (). Code traced here, there is no way to judge the real reason for this happening here. To understand. NET underlying mechanism of the classmate, can tell me.

To resolve a problem where reflection cannot be judged by isdefined (), you can call the MethodInfo GetCustomAttribute () method. For example:

Copy Code code as follows:
private static bool Isappliedwith (this MethodInfo MethodInfo, Type attributetype, String attributename)
{
Return Methodinfo.getcustomattributes (AttributeType, false). ToString (). Contains (AttributeName);
}

Whether the use of reflection loading, or using TypeOf, this way to determine if the method used the specified attribute, can be effective.

The above is c#isdefined the whole content of the question, hope can give everybody a reference, also hope everybody support cloud-dwelling community a lot.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.