Find Methods That Have Custom Attribute Using Reflection

Find methods that have custom attribute using reflection

Your code is completely wrong.

You are looping through every type that has the attribute, which will not find any types.

You need to loop through every method on every type and check whether it has your attribute.

For example:

var methods = assembly.GetTypes()
.SelectMany(t => t.GetMethods())
.Where(m => m.GetCustomAttributes(typeof(MenuItemAttribute), false).Length > 0)
.ToArray();

How do I use MSTest without Visual Studio?

It doesn't have a GUI (apart from Visual Studio) but there's a command line tool: MSTest.exe

Here is the official documentation on running MSTest tests.

Getting all methods with custom attribute in c# never finds a method

The default forGetMethods is "anything public", but your method is non-method. Try adding BindingFlags.NonPublic | BindingFlags.Static to the GetMethods call, to give it more of a hint.

C# Reflection : Finding Custom Attributes on a Class Property

Your attribute is being applied to the properties. While you do have fields, they're created by the compiler - and your custom attribute isn't being applied to them. Your code is broadly equivalent to:

[CompilerGenerated]
private List<string> list1;

[Includable]
public List<string> List1 { get { return list1; } set { list1 = value; } }

[CompilerGenerated]
private List<string> list2;
public List<string> List2 { get { return list2; } set { list2 = value; } }

As you can see, there are still two fields (list1 and list2) - which is why your first assertion succeeds. However, neither of those have the Includable attribute. It's the List1 property which does.

So you need to be asking for the properties rather than the fields. I'd also do this with LINQ rather than looping explicitly:

var properties = typeof(TestClass)
.GetProperties(BindingFlags.NonPublic |
BindingFlags.Public |
BindingFlags.Instance)
.Where(p => p.IsDefined(typeof(IncludableAttribute), true)
.ToList();

(You may want to consider whether you really want to pick up non-public properties.)

C# get property value from object using custom attribute

In your example, you're getting the customattributes of the class instead of the properties.

Here is an example:

private object GetValueBySectionFlag(object obj, string flagName)
{
// get the type:
var objType = obj.GetType();

// iterate the properties
var prop = (from property in objType.GetProperties()
// iterate it's attributes
from attrib in property.GetCustomAttributes(typeof(SectionFlagAttribute), false).Cast<SectionFlagAttribute>()
// filter on the name
where attrib.Name == flagName
// select the propertyInfo
select property).FirstOrDefault();

// use the propertyinfo to get the instance->property value
return prop?.GetValue(obj);
}

Note: this will return only the first property which contains the SectionFlagAttribute with the right name. You could modify the method to return multiple values. (like a collection of propertyname/value)


Usage:

// a test instance.
var obj = new ApplicationStatusFlags { IceAttributeStatement = true };

// get the value by SectionFlag name
var iceAttributeStatement = GetValueBySectionFlag(obj, "APPLICANTSTATEMENT");

If the returned value is null then the flag is not found or the property's value is null.

How to figure out dynamically all methods with custom attribute

The simplest approach is to use MethodInfo.IsDefined - quite possibly with LINQ as well:

var testMethods = from assembly in assemblies
from type in assembly.GetTypes()
from method in type.GetMethods()
where method.IsDefined(typeof(TestMethodAttribute))
select method;

foreach (var method in testMethods)
{
Console.WriteLine(method);
}

(I'd do all the sorting with LINQ as well. Obviously you can tune the GetMethods call etc to only return instance methods, for example.)

It's not entirely clear to me why your current approach doesn't work or why it does work when you've created instances - but without a short but complete example demonstrating the problem, it would be hard to diagnose it further. I'd definitely start with the code above :)

Issue finding custom attributes

You can do this to fetch a list of all methods using the RPCAttributeType

var results = assembly.GetTypes().SelectMany(t => t.GetMethods())
.Where(m => m.GetCustomAttributes(typeof(RPCAttributeType), true).Count() > 0).ToList();

EDIT: BASED ON COMMENT

When assigning assembly use

var assembly = Assembly.GetExecutingAssembly();

Adding custom attributes to ReturnParameter using reflection or IL

You need to apply the custom attributes to the return value parameter, not the method:

ParameterBuilder returnValueParameterBuilder = 
methodBuilder.DefineParameter(0, ParameterAttributes.Retval, parameterName: null);

foreach (var attribute in OriginalMethod.ReturnParameter.GetCustomAttributesData()
.ToCustomAttributeBuilder())
{
returnValueParameterBuilder.SetCustomAttribute(attribute);
}


Related Topics



Leave a reply



Submit