Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8306741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:23:26+00:00 2026-06-08T18:23:26+00:00

I was pretty sure that this was possible, but for some reason, I can’t

  • 0

I was pretty sure that this was possible, but for some reason, I can’t seem to figure this out… I am trying to make an extension method off of Type, that will take in a Func to a property from that type, and extract a DefaultValue from the DefaultValueAttribute.

I can get it working, but only if I specify the type arguments for the GetDefaultValue function call. Here is my code as I have it currently:

Person entity:

public class Person
{
    public string FirstName { get; set; }

    [DefaultValue("1234")]
    public string DOB { get; set; }
}

Calls to the method:

//Messing around in LinqPad - .Dump() is LinqPad method

//Works
//typeof(Person).GetDefaultValue<Person, string>(x=>x.DOB).Dump();

//Trying to get to work
//Can't figure out how to get it to infer that TIn is of the Type type.....
typeof(Person).GetDefaultValue(x=> x.DOB).Dump();

This is where the method calls are going to… I am just trying to figure out the means right now before I incorporate into my actual program… Error checking will come into play once I’ve figured out either how to do it, or to give up b/c it can’t be done…

public static class Extensions
{
    //    Works
    //    public static TProperty GetDefaultValue<TModel, TProperty>(this Type type, Expression<Func<TModel, TProperty>> exp)
    //    {
    //        var property = typeof(TModel).GetProperties().ToList().Single(p => p.Name == GetFullPropertyName(exp));
    //        var defaultValue = (DefaultValueAttribute)property.GetCustomAttributes(typeof(DefaultValueAttribute), false).FirstOrDefault();
    //        return (TProperty)defaultValue.Value;
    //    }

    //trying to get to work
    //I know that I can't do the following, but it is basically what I am trying to do...  I think!
    public static TProperty GetDefaultValue<TProperty>(this Type type, Expression<Func<typeof(type), TProperty>> exp) 
    {
        var property = type.GetProperties().ToList().Single(p => p.Name == GetFullPropertyName(exp));
        var defaultValue = (DefaultValueAttribute)property.GetCustomAttributes(typeof(DefaultValueAttribute), false).FirstOrDefault();
        return (TProperty)defaultValue.Value;
    }

    //GetFullPropertyName c/o: http://stackoverflow.com/users/105570/
    //ref: http://stackoverflow.com/questions/2789504/
    public static string GetFullPropertyName<TModel, TProperty>(Expression<Func<TModel, TProperty>> exp)
    {
        MemberExpression memberExp;

        if (!TryFindMemberExpression(exp.Body, out memberExp))
               return String.Empty;

        var memberNames = new Stack<string>();
        do
            memberNames.Push(memberExp.Member.Name);
        while (TryFindMemberExpression(memberExp.Expression, out memberExp));

        return String.Join(".", memberNames.ToArray());
    }

    private static bool TryFindMemberExpression(Expression exp, out MemberExpression memberExp)
    {
        memberExp = exp as MemberExpression;
        if (memberExp != null)
            return true;

        if (IsConversion(exp) && exp is UnaryExpression)
        {
            memberExp = ((UnaryExpression)exp).Operand as MemberExpression;
            if (memberExp != null)
                return true;
        }    

        return false;
    }

    private static bool IsConversion(Expression exp)
    {
        return exp.NodeType == ExpressionType.Convert || exp.NodeType == ExpressionType.ConvertChecked;
    }
}

Am I crazy, or is this actually possible? Thank you in advance for your help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T18:23:27+00:00Added an answer on June 8, 2026 at 6:23 pm

    That’s not how it works- typeof(Person) doesn’t have a property called DOB, but a class whose type is Person does. What you want is to make your extension method generic:

    public static TValue GetDefaultValue<TClass, TValue>(this TClass val, Expression<Func<TClass, TValue>> getter) {
        var type = typeof(TClass);
        var property = type.GetProperties().ToList().Single(p => p.Name == GetFullPropertyName(exp));
        var defaultValue = (DefaultValueAttribute)property.GetCustomAttributes(typeof(DefaultValueAttribute), false).FirstOrDefault();
        return (TProperty)defaultValue.Value;
    }
    

    and call it something like:

    Person somePerson = GetMeAPerson();
    somePerson.GetDefaultValue(p=>p.DOB);
    

    Note that I have not tested the above, but I’ve seen similar code in the past that worked. That said, I suspect that what you were originally trying to do isn’t very appealing this way because you need to make a Person instance first.

    Another, possibly more appealing approach is to not make it an extension method at all:

    public static TValue GetDefaultValue<TClass, TValue>(Expression<Func<TClass, TValue>> getter) {
        var type = typeof(TClass);
        var property = type.GetProperties().ToList().Single(p => p.Name == GetFullPropertyName(exp));
        var defaultValue = (DefaultValueAttribute)property.GetCustomAttributes(typeof(DefaultValueAttribute), false).FirstOrDefault();
        return (TProperty)defaultValue.Value;
    }
    

    Then you can call it without an instance, but the inference isn’t as nice):

    var defaultValue = GetDefaultValue<Person, DateTime>(p => p.DOB);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Pretty sure there is an easy answer to this, but just can't find the
I'm pretty sure this is impossible, but. . . I am trying to get
I'm pretty sure this is a duplicate question, but I can't find the original(s).
Ok, so I'm pretty sure this isn't even possible, but I have a friend
I'm pretty sure there is an easier way to do this that I'm missing
I have this annoying problem, that Im pretty sure happens to all of you
I'm hoping (and pretty sure that) someone out there is much better at MySQL
Pretty sure this has been asked already, but I don't know what to search
Pretty sure some people encountered this problem before. Followed all the instructions to setup
I'm pretty sure this is not possible in Zend Framework (I have searched the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.