2016-10-27 147 views
-1

也許一個很簡單的問題,但我有一個初始化的類對象,通過參數傳遞給另一個類。然後讀取類對象以檢查其成員的值,如果不正確,可能會更改它們。遍歷所有類屬性

我可以訪問單個屬性並更改它們的值,但我想要做的是循環所有整數類型屬性以檢查其基本值並在必要時進行更改。

這裏是對象結構的一個例子:

+ Prices 
++ MainPrices 
+++ RelevantPrices 
++++ (Int) Price 

+ SubPrices 
++ MainPrices 
+++ RelevantPrices 
++++ (Int) Price 

+Rooms 
++ Data 
+++ (String) Name 
+++ (Int) NameType 
+++ (String) Location 
+++ (Int) RoomNumber 

我需要做的就是讓Rooms.Data和循環遍歷所有的int類型的參數。我試過使用反射,但爲此我需要一個新的實例引用類型,我所擁有的就是這裏的初始化類對象。

任何人都可以建議什麼可能是最好的方式來循環和有條件地改變他們現有的價值嗎?

編輯:

繼承人一些示例代碼:

public class Test() 
{ 

public Void Init(MyClassObject Data) 
{ 

//Data is initialised with data, it has the structure explained in the original description 

//What i need to do is loop over the initialised objects properties here in this Init method, 
} 

} 
+1

你說的意思是什麼「我需要一個新的實例參考類型「? – Amy

+1

目前還不清楚」新實例引用「與」初始化類對象「是什麼意思。[mcve]真的有幫助... –

+1

」但爲此,我需要類型的新實例引用「 - 不,您不需要新的引用。使用您已有的參考文獻,即具有「現有值」的參考文獻。 – Quantic

回答

0

這會給你一個整數屬性的列表

var integers = this.GetType().GetProperties().Where(p => p.PropertyType == typeof(int)).ToList();