简体   繁体   English

用反射制作新物体?

[英]Make new object with reflection?

I'm not sure if this is possible and after lengthy research I haven't found something conclusive. 我不确定这是否可行,经过长时间的研究,我还没有得出结论。

I am trying to dynamically create a new object (itself a new Type) from a dictionary. 我试图从字典中动态创建一个新对象(本身就是一个新的Type)。 So say I have key and value that key and value will become a property that returns the value. 假设我拥有键和值,则键和值将成为返回值的属性。 Something that I can use like this: 我可以这样使用:

Sample code 样例代码

public T getObject(Dictionary<string, string> myDict)
{
 // make a new object type with the keys and values of the dictionary. 
 // sample values in dictionary:
 // id : 112345
 // name: "greg"
 // gender: "m"
 // Eventually also make the interface?
}

// Somewhere else:
var myNewObject = helper.getObject();
// what is expected here is that when I type myNewObject. 
// I get id, name, gender as suggestions
int id = myNewObject.Id;

What is important for me is to get intellisense out of it. 对我来说重要的是要从中获得智慧。 So I can type object. 所以我可以输入object. and suggestions with all the keys will come out. 所有键的建议都会出来。 This way I don't need to know the dictionary in advance in order to access the values (or else I would simply use the dictionary). 这样,我不需要事先知道字典即可访问值(否则我将只使用字典)。

I have looked into Clay but it isn't clear to me how to use it to get intellisense as I want it. 我已经研究了Clay,但是我不清楚如何使用它来获取智能感知。 I also found this post about it. 我也发现了有关它的帖子

Additionally I checked ImpromptuInterface but for both the documentation is poor or I can't figure it out. 另外,我检查了ImpromptuInterface,但由于两个文档都很差,或者我无法弄清楚。

If this is possible with Clay (it sure seems like it) how could I do it so It can work as a library on other projects that won't reference Clay? 如果Clay可以做到这一点(看起来确实如此),我该怎么做,以便它可以作为不引用Clay的其他项目上的库?

If something is unclear or this is a duplicate don't hesitate to comment. 如果不清楚或重复,请不要犹豫。

To allow IntelliSense to work, the resulting type must be known at compile-time . 要使IntelliSense正常工作,必须在编译时知道结果类型。 So at the time you're working with object , you must know its concrete type (or, at least, a type or interface as concrete as you need - having all the methods and properties you want to access). 因此,在使用object ,您必须知道其具体类型(或者至少是您所需要的具体类型或接口-具有要访问的所有方法和属性)。

It's hard to see what you're actually trying to do, but if you do know the type at compile-time, you can make your helper method generic: 很难看到您实际上要做什么,但是如果您确实在编译时知道类型,则可以使辅助方法通用:

public T GetObject<T>(Dictionary<...> dictionary)

This allows you to specify the type you're expecting at the call site: 这使您可以指定呼叫站点所需的类型:

var obj = GetObject<SomeType>(dictionary);

If this does not sound reasonable to you, you could also use an explicit cast: 如果这听起来不合理,您也可以使用显式强制转换:

var obj = (SomeType)helper.getObject();

If you don't know the concrete type, you'll at least need to have a common interface (or a base class) you can use. 如果您不知道具体的类型,则至少需要具有可以使用的公共接口(或基类)。 The runtime generated type would then be created to implement this interface. 然后将创建运行时生成的类型以实现此接口。 But you'll only ever be able to access the members that are known at compile time. 但是,你永远只能够访问那些在编译时已知的成员。 How would the compiler know all the possible keys that could exist in your dictionary at runtime? 编译器将如何知道运行时字典中可能存在的所有可能键? You're trying to code against some kind of contract - so formalize that contract in an interface (for example), and make the runtime generated type implement this interface. 您正在尝试针对某种合同进行编码-因此(例如)在接口中将该合同形式化,并使运行时生成的类型实现此接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM