用反射来制作新物体? [英] Make new object with reflection?

查看:210
本文介绍了用反射来制作新物体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这是否可能,经过漫长的研究,我还没有发现一些结论。



我正在尝试从字典动态创建一个新对象(本身是一个新的类型)。所以说我有键和值,键和值将成为返回值的属性。可以这样使用的东西:



示例代码

  public T getObject(Dictionary< string,string> myDict)
{
//使用字典的键和值创建一个新的对象类型。
//字典中的示例值:
// id:112345
// name:greg
// gender:m
//最后还做界面?
}

//其他地方:
var myNewObject = helper.getObject();
//这里预期的是当我键入myNewObject。
//我将id,name,gender作为建议
int id = myNewObject.Id;

对我来说重要的是让Intellisense退出。我可以输入对象。,所有键的建议都会出来。这样我就不需要提前知道字典,以便访问这些值(否则我只会使用字典)。



我已经研究了< a href =http://weblogs.asp.net/bleroy/clay-malleable-c-dynamic-objects-part-2 =nofollow> 粘土 ,但它对我来说不清楚如何使用它来获得智能感知,因为我想要它。我还发现 这篇文章



此外,我查看了 即兴面孔 但是对于这两个文件很差,或者我无法弄清楚。



如果这是可能的,Clay(它肯定是这样),我该怎么做,所以它可以作为一个图书馆在其他项目,不会引用Clay?



如果有什么不清楚或者这是重复的,请不要犹豫评论。


解决方案

要允许IntelliSense工作,生成的类型必须在编译时才知道。所以当你使用对象时,你必须知道它的具体类型(或者至少是你需要的具体类型或接口) - 拥有所有的方法您要访问的属性。)



很难看到您实际想要做什么,但如果您 知道类型在编译时,你可以使你的帮助方法泛型:

  public T GetObject< T>(Dictionary& ;字典)

这允许您指定您在通话网站期望的类型: p>

  var obj = GetObject< SomeType>(dictionary); 

如果这对您听起来不合理,您还可以使用显式转换:

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

如果您不知道具体类型,您至少需要有一个公共接口(或基础类)可以使用。然后将创建运行时生成的类型来实现此接口。但是,您只能在编译时访问 已知的成员。编译器如何知道运行时字典中可能存在的所有可能的键?您正在尝试对某种类型的合同进行编码 - 从而在接口(例如)中形式化该合同,并使运行时生成的类型实现此接口。


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. 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. 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. 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.

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?

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. 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).

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.

这篇关于用反射来制作新物体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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