返回基于变量的对象 [英] Return object base on variable

查看:80
本文介绍了返回基于变量的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以根据变量从类中返回一个对象?

(以下示例当​​然不起作用,但我希望它能解释我想要做的事情)。 />
tx,

Robert



我的default.aspx.cs:

 PageBase p =  new  PageBase(); 
var myObject = p.GetObjectByVar( 汽车);



我的班级:

命名空间GetObjectByVar 
{
public class PageBase
{
public object GetObjectByVar(string className)
{
return new className();
}
}

公共类汽车
{
public Cars(){}

public Model ModelStruct;
public struct模型
{
public string Model1 = Corvette;
public string Model2 = Mazda< /跨度>;
}
}
}

解决方案

是的,您可以使用 Activator.CreateInstance [ ^ ]按名称和包含它的程序集创建类型的实例。发布的链接提供了一个如何使用它的一个很好的例子。


不,你不能这样做。但是,如果这不是一个变量,而是属性或某个其他已知对象的字段,你可以通过反射轻松完成。



首先,你发现名称的属性或字段信息,对于已知类型:

http: //msdn.microsoft.com/en-us/library/kz0a8sxy.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/zy0d4103.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/53seyfee.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/4ek9c21e.aspx [ ^ ]。



然后,如果你有一个实例声明类,您可以获取此对象的字段值的此属性:

http://msdn.microsoft.com/en-us/library/system.reflection.fieldinfo.getvalue.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.reflection.fieldinfo.getvalue。 aspx [ ^ ]。



如何永远,这一切都是个坏主意。代码基于知道一些完全不可支持的名称。想象一下,你拼错了用字符串写的名字。编译器不会检测到问题。现在,想象一下你决定稍后重命名。普通软件将继续以完全相同的方式工作,并且......不好。



相反,您应该检查整个代码的设计。太糟糕了,你没有分享你的目标,那么你可以得到一个很好的替代方案的好建议。



-SA


查看Activator类:

http://msdn.microsoft.com/en-us/library/system.activator.createinstance.aspx [ ^ ]



祝你好运!

Is it possible to return an object from a class based on a variable?
(below example doesn't work of course but I hope it explains what I am trying to do).
tx,
Robert

My default.aspx.cs:

PageBase p = new PageBase();
var myObject = p.GetObjectByVar("Cars");


my class:

namespace GetObjectByVar
{
    public class PageBase
    {
        public object GetObjectByVar(string className)
        {
            return new className();
        }
    }

    public class Cars
    {
        public Cars(){ }

        public Model ModelStruct;
        public struct Model
        {
            public string Model1 = "Corvette";
            public string Model2 = "Mazda";
        }
    }
}

解决方案

Yes, you can use Activator.CreateInstance[^] to create an instance of a type by its name and the assembly in which it is contained. The link posted provides a good example on how to use it.


No, you cannot do it. However, you if this is not a variable but a property or a field of some other, known object, you can easily do it through reflection.

First, you find a property or a field info by name, for a known type:
http://msdn.microsoft.com/en-us/library/kz0a8sxy.aspx[^],
http://msdn.microsoft.com/en-us/library/zy0d4103.aspx[^],
http://msdn.microsoft.com/en-us/library/53seyfee.aspx[^],
http://msdn.microsoft.com/en-us/library/4ek9c21e.aspx[^].

Then, if you have an instance of the declaring class, you can get this property of a field value for this object:
http://msdn.microsoft.com/en-us/library/system.reflection.fieldinfo.getvalue.aspx[^],
http://msdn.microsoft.com/en-us/library/system.reflection.fieldinfo.getvalue.aspx[^].

However, all this is a bad idea. Code based on knowing some names it totally not supportable. Imagine that you misspell the name written in a string. The compiler won't detect the problem. And now, imagine that you decided to rename something later. Normal software will continue to work in exact same way, and your… No good.

Rather, you should review overall the design of your you code. Too bad you did not share your goals, then you could get a good advice on an good alternative.

—SA


Have a look into the Activator class:
http://msdn.microsoft.com/en-us/library/system.activator.createinstance.aspx[^]

Good luck!


这篇关于返回基于变量的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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