如何找到在类DateTime类型的所有属性? [英] How do I find all properties of type DateTime in an class?

查看:145
本文介绍了如何找到在类DateTime类型的所有属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to adjust the datetime of a bunch of objects.

我想通过类的属性循环,如果类型为datetime相应调整

I'd like to loop through the properties of the class and if the type is dateTime adjust accordingly.

有没有建在善良一种描述类型我可以使用?

Is there any kind of 'describe type' built in goodness I can use?

推荐答案

您可以使用反射这一点。

您的情况可能看起来有点像这样的:

Your scenario might look somewhat like this:

    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());
    }

您要重新计算所有哺乳动物的生日。和上面的实现正在寻找这样的:

Where you want to re-calculate the Birthdays of all Mammals. And the Implementations of the above are looking like this:

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!";
    }
}



所以basicly,你需要做的就是用Relfection ,这是一个mechanizm检查类型以及获取的种类的属性和其它类似的东西在运行时间。这里是你如何10天添加上述DOB的为有一个DOB每个哺乳动物的例子

So basicly what you need to do is to use Relfection, which is a mechanizm to check types and get the types properties and other things like that in run time. Here is an example on how you add 10 days to the above DOB's for each Mammal that got a DOB.

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;
}



只要记住,使用反射可能会很慢,而且很慢一般。

Just remember that using reflection can be slow, and it is slow generally.

但是,上面会打印:

My DOB is: 2010-03-22 09:18:12
My DOB is: 2010-03-22 09:18:12
My DOB is: 2010-03-22 09:18:12
My DOB is: 2010-03-22 09:18:12
Woff!
Woff!
Woff!
My DOB is: 2010-04-01 09:18:12
My DOB is: 2010-04-01 09:18:12
My DOB is: 2010-04-01 09:18:12
My DOB is: 2010-04-01 09:18:12
Woff!
Woff!
Woff!

这篇关于如何找到在类DateTime类型的所有属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆