Hryhorii
Hryhorii

Reputation: 1243

.NET Reflection. Get methods by attribute name

In my MVC 3 project I have method, that scans folder bin at dll and some loads. Then I filtered and get list Controller class. Then I filtered and try get list the methods who return ActionResult. But I get duplication methods. I try filtered an by Attribute. But nothing not obtained

private void GetControllers()
{
        IEnumerable<FileInfo> files = this.GetFileList();

        foreach (var fileInfo in files)
        {
            if (fileInfo.Name != "SGN.Framework.dll" && fileInfo.Name != "SGN.Controls.dll")
            {
                Assembly assembly = Assembly.LoadFile(fileInfo.FullName);
                AssemblyName asamName = assembly.GetName();
                IList<Type> myType =
                    assembly.GetTypes().Where(item => item.Name.Contains("Controller")).Where(
                        item => item.Name != "AdminsController" && item.Name != "ModuleController").ToList();

                foreach (var type in myType)
                {
                    var m =
                        type.GetMethods().Where(
                            item =>
                            item.ReturnType == typeof(ActionResult)).Except(type.GetCustomAttributes(true).Where(i => i != typeof(ActionInfoAttribute)));
                }
            }
        }
}

Upvotes: 0

Views: 793

Answers (1)

Victor Sharovatov
Victor Sharovatov

Reputation: 633

I know that the question is old. But maybe my answer will help someone. For this case the following code will work:

type.GetMethods().Where(
    item =>
    item.ReturnType == typeof(ActionResult) && item.IsDefined(typeof(ActionInfoAttribute), false));

Upvotes: 1

Related Questions