C#反射和实例化-是否可以执行Activator.CreateInstance(myType){X = x}? [英] C# reflection and instantiation - is there a way to do Activator.CreateInstance(myType){ X = x }?

查看:201
本文介绍了C#反射和实例化-是否可以执行Activator.CreateInstance(myType){X = x}?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这种代码的术语,但是我想知道是否可以在括号后实例化变量,但是可以使用反射.

I'm not sure of the terminology for this kind of code, but I want to know if it's possible to instantiate variables after the parentheses, but whilst using reflection.

我有一个从XML文件加载的地图.这是(int X,int Y,字符串S)的集合,其中X,Y是某个地形的位置,S是表示地形类型的字符串.我有一个字典,可在字符串和相关类型之间传递;例如,一对键值对可能是"Tree",typeof(Tree).

I have a map which gets loaded from an XML file. This is a collection of (int X, int Y, string S) where the X,Y is the position of some terrain, and S is a string representing the type of the terrain. I have a dictionary to pass between the strings and the relevant types; for example one key-value pair might be "Tree", typeof(Tree).

在使用反射时,尽管我知道可以用参数实例化,但我唯一能接受的方法就是使用Activator.CreateInstance(Type t),即使用空的构造函数.

When using reflection, although I know it's possible to instantiate with parameters, the only way I'm comfortable is just by using Activator.CreateInstance(Type t), i.e. with an empty constructor.

当我对地图进行硬编码时,我最初会像这样实例化(在一些i,j for循环内):

When I had the maps hard coded, I would originally instantiate like this (within some i,j for loop):

case: "Tree"
 world.Add( new Tree(i,j) );

在开始考虑反射和保存文件时,我将其更改为:

Whilst starting to think about reflection and my save file, I changed this to:

world.Add( new Tree() { X = i, Y = j }

但是,我意识到这不适用于反射,因此我必须执行以下操作(Tree继承自Terrain,而字典仅将XML保存数据字符串转换为类型):

However, I realised that this won't work with reflection, so I am having to do the following (Tree inherits from Terrain, and the dictionary just converts the XML save data string to a type):

Type type = dictionary[dataItem.typeAsString];
Terrain t = (Terrain)Activator.CreateInstance(type);
t.X = i;
t.Y = j;
world.Add(t);

我宁愿使用

Type type = dictionary[dataItem.typeAsString];
world.Add((Terrain)Activator.CreateInstance(type) { X = i, Y = j }

是否有这样的快捷方式?我想我是否可以编辑world.Add以取一个X和Y并转换为Terrain来访问这些变量,但我仍然对a)这种{var1 = X,var2 = Y}编程的名称感到好奇,以及b)使用反射时是否存在类似的东西.

Is there any shortcut like this? I guess if not I could edit world.Add to take an X and Y and cast to Terrain in there to access those variables, but I am still curious as to a) what this {var1 = X, var2 = Y} programming is called, and b) whether something similar exists when using reflection.

推荐答案

此语法称为对象初始化器"语法,仅是用于设置属性的语法糖.

This syntax is called Object Initializer syntax and is just syntactic sugar for setting the properties.

代码var result = new MyType { X = x }将被编译为此:

MyType __tmp = new MyType();
__tmp.X = x;
MyType result = __tmp;

如果仅在运行时知道实例化的类型,则必须自己使用PropertyInfo.SetValue进行操作;如果在编译时知道该类型,则必须使用常规属性设置器.

You will have to do that yourself using PropertyInfo.SetValue if you know the instantiated type only at runtime or use the normal property setters if the type is known at compile time.

这篇关于C#反射和实例化-是否可以执行Activator.CreateInstance(myType){X = x}?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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