C# 如何在类中查找DateTime类型的所有属性?

C# 如何在类中查找DateTime类型的所有属性?,c#,datetime,C#,Datetime,我需要调整一组对象的日期时间 我想遍历类的属性,如果类型是dateTime,则相应地进行调整 有什么“描述类型”的内在优点我可以使用吗?查找反射,但基本上是这样做的 GetType().GetProperties(…实例|…公共)并获得定义的属性列表。。检查属性的值类型,并将其与typeof(DateTime)进行比较。它称为反射 var t = this; var props = t.GetType().GetProperties(); foreach (var prop in props)

我需要调整一组对象的日期时间

我想遍历类的属性,如果类型是dateTime,则相应地进行调整


有什么“描述类型”的内在优点我可以使用吗?

查找反射,但基本上是这样做的


GetType().GetProperties(…实例|…公共)并获得定义的属性列表。。检查属性的值类型,并将其与typeof(DateTime)进行比较。

它称为反射

var t = this;
var props = t.GetType().GetProperties();
foreach (var prop in props)
{
    if (prop.PropertyType == typeof(DateTime))
    {
        //do stuff like prop.SetValue(t, DateTime.Now, null);

    }
}
你可以用这个

您的场景可能看起来有点像这样:

    static void Main(string[] args)
    {
        var list = new List<Mammal>();

        list.Add(new Person { Name = "Filip", DOB = DateTime.Now });
        list.Add(new Person { Name = "Peter", DOB = DateTime.Now });
        list.Add(new Person { Name = "Goran", DOB = DateTime.Now });
        list.Add(new Person { Name = "Markus", DOB = DateTime.Now });

        list.Add(new Dog { Name = "Sparky", Breed = "Unknown" });
        list.Add(new Dog { Name = "Little Kid", Breed = "Unknown" });
        list.Add(new Dog { Name = "Zorro", Breed = "Unknown" });

        foreach (var item in list)
            Console.WriteLine(item.Speek());

        list = ReCalculateDOB(list);

        foreach (var item in list)
            Console.WriteLine(item.Speek());
    }
internal interface Mammal
{
    string Speek();
}

internal class Person : Mammal
{
    public string Name { get; set; }
    public DateTime DOB { get; set; }

    public string Speek()
    {
        return "My DOB is: " + DOB.ToString() ;
    }
}
internal class Dog : Mammal
{
    public string Name { get; set; }
    public string Breed { get; set; }

    public string Speek()
    {
        return "Woff!";
    }
}
var query = from property in typeof(HasDateTimes).Properties()
            where property.Type() == typeof(DateTime)
            select p;
Array.ForEach( query.ToArray(), p => Console.WriteLine( p.Name ) );
因此,基本上您需要做的是使用relefection,这是一种在运行时检查类型并获取类型属性和其他类似内容的机制。下面是一个例子,说明如何为每只获得DOB的哺乳动物在上述DOB的基础上增加10天

static List<Mammal> ReCalculateDOB(List<Mammal> list)
{
    foreach (var item in list)
    {
        var properties = item.GetType().GetProperties();
        foreach (var property in properties)
        {
            if (property.PropertyType == typeof(DateTime))
                property.SetValue(item, ((DateTime)property.GetValue(item, null)).AddDays(10), null);
        }
    }

    return list;
}

要快速回答和指向该问题的提示,并且如果您有可用的PowerShell(Vista/Windows 7,Windows 2008已经安装了),您只需启动控制台和DateTime,例如

Get-Date | Get-Member
它将列出DateTime实例的成员。您还可以查看静态成员:

Get-Date | Get-Member -Static

如果您关心您可能感兴趣的反射对性能的影响,那么可以创建一个库,使查询和访问成员更容易、更快

对于instace,MaxGuernseyIII的代码可能会使用Fasterflect重写,如下所示:

    static void Main(string[] args)
    {
        var list = new List<Mammal>();

        list.Add(new Person { Name = "Filip", DOB = DateTime.Now });
        list.Add(new Person { Name = "Peter", DOB = DateTime.Now });
        list.Add(new Person { Name = "Goran", DOB = DateTime.Now });
        list.Add(new Person { Name = "Markus", DOB = DateTime.Now });

        list.Add(new Dog { Name = "Sparky", Breed = "Unknown" });
        list.Add(new Dog { Name = "Little Kid", Breed = "Unknown" });
        list.Add(new Dog { Name = "Zorro", Breed = "Unknown" });

        foreach (var item in list)
            Console.WriteLine(item.Speek());

        list = ReCalculateDOB(list);

        foreach (var item in list)
            Console.WriteLine(item.Speek());
    }
internal interface Mammal
{
    string Speek();
}

internal class Person : Mammal
{
    public string Name { get; set; }
    public DateTime DOB { get; set; }

    public string Speek()
    {
        return "My DOB is: " + DOB.ToString() ;
    }
}
internal class Dog : Mammal
{
    public string Name { get; set; }
    public string Breed { get; set; }

    public string Speek()
    {
        return "Woff!";
    }
}
var query = from property in typeof(HasDateTimes).Properties()
            where property.Type() == typeof(DateTime)
            select p;
Array.ForEach( query.ToArray(), p => Console.WriteLine( p.Name ) );
Fasterflect使用轻量级代码生成来加快访问速度(提高2-5倍,如果直接缓存和调用生成的委托,则速度接近本机速度)。查询成员通常更容易、更方便,但不会更快。请注意,这些数字不包括JIT编译生成代码的显著初始开销,因此性能提升只在重复访问时可见


免责声明:我是该项目的参与者。

嘿,这个问题有点老了,但你可以用这个:

在类内(选择所有值)-设置值将在没有强制转换的情况下运行,这只是为了选择:

(from property in this.GetType().GetProperties()
                    where property.PropertyType == typeof(DateTime)
                    select property.GetValue(this)).Cast<DateTime>()
(来自此.GetType().GetProperties()中的属性)
其中property.PropertyType==typeof(DateTime)
选择property.GetValue(this)).Cast()
课外活动包括:

var instance = new MyClass();
var times = (from property in instance.GetType().GetProperties()
                        where property.PropertyType == typeof(DateTime)
                        select property.GetValue(instance)).Cast<DateTime>()
var instance=new MyClass();
var times=(来自instance.GetType().GetProperties()中的属性)
其中property.PropertyType==typeof(DateTime)
选择property.GetValue(实例)).Cast()
为了获得最大日期时间值,我运行以下命令

var lastChange = (from property in this.GetType().GetProperties()
                        where property.PropertyType == typeof(DateTime)
                        select property.GetValue(this)).Cast<DateTime>().Max();
var lastChange=(来自this.GetType().GetProperties()中的属性)
其中property.PropertyType==typeof(DateTime)
选择property.GetValue(this)).Cast().Max();

非常感谢。哦,这次我没想到林克:)哇,已经把答案标记为正确了。但是你的帮助挽救了这一天-啦啦队你会修改它以使用任何对象吗