2010-03-22 132 views
17

我需要調整一堆對象的日期時間。如何查找類中DateTime類型的所有屬性?

我想遍歷類的屬性,如果類型是dateTime調整相應。

是否有任何一種'描述類型'內置的善良我可以使用?

+0

以什麼方式調整? – 2010-03-22 08:05:00

+0

添加若干小時。 – Chin 2010-03-22 08:11:17

回答

18

您可以使用reflection這一點。

你的情況可能看起來有點像這樣:如果你想

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

重新計算所有哺乳動物的生日。所以basicly你需要做什麼

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

是使用Relfection,這是一個mechanizm檢查類型和獲得類型的屬性和其他東西一樣,在運行時:和上面的實現正在尋找這樣的。下面是一個例子,說明如何爲每個獲得DOB的哺乳動物添加10天以上的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; 
} 

請記住,使用反射速度可能會很慢,而且通常會很慢。

然而,上面會打印:

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! 
+0

哇,已經標記了一個答案是正確的。但你的幫助節省了一天的時間 - 歡呼 – Chin 2010-03-22 08:56:02

+0

你會如何修改這個使用任何對象 – 2017-02-28 18:28:47

1

仰望反映,basicly你這樣做

obj.GetType()GetProperties中。(..實例| ..Public)和你有定義的屬性列表..檢查屬性的值類型並將其與typeof(DateTime)進行比較。

11

它被稱爲反射。

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

    } 
} 
+0

非常感謝。將考慮反思。 – Chin 2010-03-22 08:05:44

3
class HasDateTimes 
{ 
    public DateTime Foo { get; set; } 
    public string NotWanted { get; set; } 
    public DateTime Bar { get { return DateTime.MinValue; } } 
} 

static void Main(string[] args) 
{ 
    foreach (var propertyInfo in 
    from p in typeof(HasDateTimes).GetProperties() 
     where Equals(p.PropertyType, typeof(DateTime)) select p) 
    { 
    Console.WriteLine(propertyInfo.Name); 
    } 
} 
+1

+1對於linq解決方案 – Axarydax 2010-03-22 08:16:57

+0

哦,我沒有想到LINQ這次:) – 2010-03-22 08:19:27

0

對於非常快速的答案和指導這個問題,如果你有可用的PowerShell(VISTA/Windows 7中,Windows 2008中已經得到它安裝了),你可以只需啓動控制檯和DateTime即可做

Get-Date | Get-Member 

這將列出你的DateTime實例的成員。您還可以看看靜態成員:

Get-Date | Get-Member -Static 
0

如果您擔心反射對性能的影響可能會在Fasterflect感興趣,圖書館,使查詢和訪問成員更容易和更快。

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倍的因素,或接近本機:

對於instace,MaxGuernseyIII的代碼可能會使用Fasterflect這樣改寫如果您直接緩存並調用生成的委託,則會提高速度)。查詢會員通常更容易,更方便,但不會更快。請注意,這些數字不包括JIT編譯生成代碼的重要初始開銷,因此只有重複訪問才能看到性能增益。

免責聲明:我是該項目的貢獻者。

0

嘿,這問題是大一點,但你可以這樣做:

類中(選擇所有值) - 設定值將沒有它單曲投僅運行選擇:

(from property in this.GetType().GetProperties() 
        where property.PropertyType == typeof(DateTime) 
        select property.GetValue(this)).Cast<DateTime>() 

外的類將是:

var instance = new MyClass(); 
var times = (from property in instance.GetType().GetProperties() 
         where property.PropertyType == typeof(DateTime) 
         select property.GetValue(instance)).Cast<DateTime>() 

爲了得到最大DATETIME值運行此

var lastChange = (from property in this.GetType().GetProperties() 
         where property.PropertyType == typeof(DateTime) 
         select property.GetValue(this)).Cast<DateTime>().Max(); 
相關問題