Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 返回DefaultValue的对象属性上的自定义扩展方法_C#_.net_Extension Methods - Fatal编程技术网

C# 返回DefaultValue的对象属性上的自定义扩展方法

C# 返回DefaultValue的对象属性上的自定义扩展方法,c#,.net,extension-methods,C#,.net,Extension Methods,我想创建一个自定义扩展,它将处理对象T的属性,而不管属性的类型如何。我需要扩展来获取DefaultValue属性的值 看看下面的课程,我希望能够做到以下几点: Employee employee = new Employee(); string defaultNationality = employee.employeeNationality.GetDefaultValue(); var deualtValue = someObject.GetDefaultValue(x=>x.Some

我想创建一个自定义扩展,它将处理对象T的属性,而不管属性的类型如何。我需要扩展来获取DefaultValue属性的值

看看下面的课程,我希望能够做到以下几点:

Employee employee = new Employee();
string defaultNationality = employee.employeeNationality.GetDefaultValue();
var deualtValue = someObject.GetDefaultValue(x=>x.SomeProperty);
其中,员工定义为

public class Employee
{
    [Browsable(false)]
    public int employeeKey { get; set; }

    [DisplayName("Name")]
    [Category("Design")]
    [Description("The name of the employee.")]
    public string employeeName { get; set; }

    [DisplayName("Active")]
    [Category("Settings")]
    [Description("Indicates whether the employee is in active service.")]
    [DefaultValue(true)]
    public bool employeeIsActive { get; set; }

    [DisplayName("Nationality")]
    [Category("Settings")]
    [Description("The nationality of the employee.")]
    [DefaultValue("Dutch")]
    public string employeeNationality { get; set; }
}
您需要使用方法来获取所需的属性值。 例如 您可以将所需的扩展方法定义为

using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

public static class Extensions
{
    public static T GetDefaultValue<S,T>(this S source,Expression<Func<S,T>> expression)
    {
        var body = expression.Body as MemberExpression;
        if(body.Member.GetCustomAttributes<DefaultValueAttribute>().Any())
        {
             return (T)body.Member.GetCustomAttribute<DefaultValueAttribute>().Value;
        }
        return default;
    }
}
您需要使用方法来获取所需的属性值。 例如 您可以将所需的扩展方法定义为

using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

public static class Extensions
{
    public static T GetDefaultValue<S,T>(this S source,Expression<Func<S,T>> expression)
    {
        var body = expression.Body as MemberExpression;
        if(body.Member.GetCustomAttributes<DefaultValueAttribute>().Any())
        {
             return (T)body.Member.GetCustomAttribute<DefaultValueAttribute>().Value;
        }
        return default;
    }
}

您可以使用这样的扩展方法:

using System;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
public static class ObjectExtensions
{
    public static K GetDefaultValue<T, K>(this T obj, Expression<Func<T, K>> exp)
    {
        var info = ((MemberExpression)exp.Body).Member;
        return (K)(TypeDescriptor.GetProperties(info.DeclaringType)[info.Name]
            .Attributes.OfType<DefaultValueAttribute>()
            .FirstOrDefault()?.Value ?? default(K));
    }
}

该方法尝试基于DefaultValue属性获取属性的默认值,如果属性没有此类属性,则返回属性类型的默认值,例如,对于integer属性,如果找不到DefaultValue属性,则返回0

为了获取属性和元数据,我通常使用TypeDescriptor,因为它更灵活,但是使用反射也是完全有效的


您可以使用这样的扩展方法:

using System;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
public static class ObjectExtensions
{
    public static K GetDefaultValue<T, K>(this T obj, Expression<Func<T, K>> exp)
    {
        var info = ((MemberExpression)exp.Body).Member;
        return (K)(TypeDescriptor.GetProperties(info.DeclaringType)[info.Name]
            .Attributes.OfType<DefaultValueAttribute>()
            .FirstOrDefault()?.Value ?? default(K));
    }
}

该方法尝试基于DefaultValue属性获取属性的默认值,如果属性没有此类属性,则返回属性类型的默认值,例如,对于integer属性,如果找不到DefaultValue属性,则返回0

为了获取属性和元数据,我通常使用TypeDescriptor,因为它更灵活,但是使用反射也是完全有效的


反射和Linq表达式。您希望这样做的方式将只能检查当前值并基于该值获取默认值。使用Linq表达式,可以使用反射来获取属性。ie employee.GetDefaultValuex=>x.EmployeeNational;反射和Linq表达式。您希望这样做的方式将只能检查当前值并基于该值获取默认值。使用Linq表达式,可以使用反射来获取属性。ie employee.GetDefaultValuex=>x.EmployeeNational;