Pass class property as parameter to extension method I have the following class structure:

MyAttribute.cs

public sealed class MyAttribute : Attribute
{
    public MyAttribute(string name)
    {
        if (string.IsNullOrEmpty(name))
        {
            throw new ArgumentNullException(nameof(name));
        }

        Name = name;
    }

    public string Name { get; }
}


Matrix.cs

public class Matrix
{
    [MyAttribute("awesome_name")]
    public string AwesomeName { get; set; }
}


And we make an method to "extract" the value defined on name of MyAttribute:

AttributeExtensions.cs

public static class AttributeExtensions
{
    public static string GetMyAttributeNameOfProperty&lt;TClass&gt;(Expression&lt;Func&lt;TClass, object&gt;&gt; expression)
    {
        if (expression.Body is MemberExpression memberExpression)
        {
            var propertyInfo = (PropertyInfo)memberExpression.Member;

            var hasMyAttribute = propertyInfo.IsDefined(typeof(MyAttribute), false);

            if (hasMyAttribute)
            {
                return propertyInfo.GetCustomAttribute&lt;MyAttribute&gt;().Name;
            }
        }

        return default;
    }
}


Finally, we call this way: 

AttributeExtensions.GetMyAttributeNameOfProperty&lt;Matrix&gt;(exp =&gt; exp.AwesomeName)


This call, returns: "awesome_name"

We have some doubts:


Exists an more "clean" way to pass the property of &lt;TClass&gt;(exp.AwesomeName) to this extension method?
We really need of Expression&lt;&gt;? It's possible to make this only with Func&lt;TClass, object&gt;? 


Like:

GetMyAttributeNameOfProperty&lt;TClass&gt;(Func&lt;TClass, object&gt; expression)


Any suggestions?
I'd suggest:

public static class AttributeExtensions
{
    public static string GetMyAttributeNameOfProperty&lt;TClass&gt;(string propertyName)
    {
        var propertyInfo = typeof(TClass).GetProperty(propertyName);
        return propertyInfo?.GetCustomAttribute&lt;MyAttribute&gt;()?.Name ?? default;
    }
}


Usage:

var awesome = AttributeExtensions.GetMyAttributeNameOfProperty&lt;Matrix&gt;(nameof(Matrix.AwesomeName));
Console.WriteLine(awesome);


Note:  The above isn't an extension method.  The below code is.

public static class AttributeExtensions
{
    public static string GetMyAttributeNameOfProperty&lt;TType&gt;(this TType me, string propertyName) where TType : Type
    {
        var propertyInfo = me.GetProperty(propertyName);
        return propertyInfo?.GetCustomAttribute&lt;MyAttribute&gt;()?.Name ?? default;
    }
}


Usage:

var awesome = typeof(Matrix).GetMyAttributeNameOfProperty(nameof(Matrix.AwesomeName));
Console.WriteLine(awesome);


Both output:


  awesome_name


Benefits:


Does not use an Expression or even the namespace
System.Linq.Expressions 
Won't throw an exception (returns null if property or attribute doesn't exist)
Using nameof makes refactoring safe in the event you rename class properties
Does not use Func&lt;TClass, object&gt; or similar
Does not require an instance of Matrix (or any other type since it's generic) 


Either way works and is really a matter of preference.  There are pros and cons to using extension methods.