[英]How to dynamically pass multiple object instance to another class.
I need to pass object of one class as parameter in other class dynamically. 我需要动态地将一个类的对象作为参数传递给另一类。 I have following code which has to be customize. 我有以下代码必须自定义。
ABC abc = new ABC();
abc.id = "E100";
abc.type = ABCType.BB_UNIQUE;
abc.typeSpecified = true;
ABC t = new ABC();
t.id = "I";
t.yellowkey = MarketSector.Equity;
t.yellowkeySpecified = true;
t.type = ABCType.t;
t.typeSpecified = true;
ABC abc2 = new ABC();
abc2.id = "GB";
abc2.type = ABCType.ISIN;
abc2.typeSpecified = true;
ABCs i = new ABCs();
i.abc = new abc[] { abc, abc2, t };
I was able to as follows To dynamically create the class object but i am not able to pass it to another class: 我能够如下来动态创建类对象,但无法将其传递给另一个类:
string value = "E100,I";
string[] id;
id = value.Split(',');
IDictionary<string, ABC> col = new Dictionary<string, ABC>();
foreach (string val in id)
{
col[val] = new ABC();
col[val].id = val;
}
ABCs i = new ABCs();
This part is where i am struggling i.abc = new abc[] { abc, abc2, t }; 这是我在努力的地方i.abc = new abc [] {abc,abc2,t}; How will i be able to pass dynamic object of IDictionary to i.abc? 如何将IDictionary的动态对象传递给i.abc? Any help will be appreciated Thanks 任何帮助将不胜感激谢谢
The IDictionary<TKey, TValue>
interface has a Values
property for every value contained in the IDictionary<TKey, TValue>
which returns an ICollection<TValue>
. IDictionary<TKey, TValue>
接口的IDictionary<TKey, TValue>
包含的每个值都有一个Values
属性,该IDictionary<TKey, TValue>
返回ICollection<TValue>
。
After including the System.Linq
namespace, the ICollection<T>
interface gets an extension method ToArray<T>()
which converts the collection to an array of type T
. 在包含System.Linq
命名空间之后, ICollection<T>
接口将获得扩展方法ToArray<T>()
,该方法将集合转换为类型T
的数组。 So the solution for your problem should be the following line: 因此,针对您的问题的解决方案应为以下代码行:
i.abc = col.Values.ToArray();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.