![](/img/trans.png)
[英]Is it possible to use the conditional attribute to create similliar debugger and run-time method?
[英]Is it possible to use ExpandoObject to create run-time properties?
通常,我們可以創建這樣的屬性,
dynamic expando = new ExpandoObject();
expando.Price = 45k;
expando.Value = "Good";
就我而言,直到運行時我才會知道諸如“價格”或“價值”之類的屬性。 我怎樣才能創建這樣的動態屬性。 就像是,
dynamic expando = new ExpandoObject();
expando[properties[0]] = 45k;
expando[properties[1]] = "Good";
expando[properties[2]] = "Red";
expando[properties[3]] = 8;
無論如何要實現這種行為。
只需使用它實現IDictionary<string, Object>
的事實:
IDictionary<string, Object> expando = new ExpandoObject();
expando[properties[0]] = 45;
expando[properties[1]] = "Good";
expando[properties[2]] = "Red";
expando[properties[3]] = 8;
dynamic d = expando;
// Now use the properties as normal
另一方面,如果您直到執行時才知道這些屬性,那么實際上會消耗它們的是什么? 使用ExpandoObject
可能仍然有意義 - 但同樣可以使用Dictionary<string, object>
開始。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.