动态bean实例化(反射???) [英] Dynamic bean instantiation (reflection???)

查看:69
本文介绍了动态bean实例化(反射???)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我试图根据字符串

类名动态创建对象实例,然后我需要动态设置这些值的值

对象,但我不知道如何在C#中执行此操作。以下是我需要做的例子




****** Dog.cs ******

命名空间MySystem

{

公共类狗

{

私人字符串名称;


公共字符串名称

{

get {return name; }

set {name = value; }

}

}

}


****** Cat.cs * *****

命名空间MySystem

{

公共类Cat

{

私人字符串名称;


公共字符串名称

{

get {return name; }

set {name = value; }

}

}

}


****** SetAnimals.cs * *****

命名空间MySystem

{

公共类SetAnimals

{

string [,] animals = new string [,] {{" Dog"," dogname1"},

{" Dog"," dogname2"},{" Cat" ;,catname1}};


公共IList setAnimals()

{

IList animalList = new ArrayList() ;


//以某种方式动态创建一个对象的新实例,并且

将它添加到animalList

//例如animals [0,0]是一种Dog类型,因此动态地创建一个Dog类,并为刚从动物身上创建的Dog类设置名称

// [ 0,1]

" dogname1"


返回animalList;

}

} < br $>
}


我想在C#中做些什么?有没有人有任何想法

如何做到这一点?


谢谢,

Jerry

Hi,

I am trying to dynamically create object instances based on a string
class name, and then I need to dynamically set values to these
objects, but I have no idea how to do this in C#. Here is the example
of what I need to do:

******Dog.cs******
namespace MySystem
{
public class Dog
{
private string name;

public string name
{
get { return name; }
set { name = value; }
}
}
}

******Cat.cs******
namespace MySystem
{
public class Cat
{
private string name;

public string name
{
get { return name; }
set { name = value; }
}
}
}

******SetAnimals.cs******
namespace MySystem
{
public class SetAnimals
{
string[,] animals = new string[,] {{"Dog","dogname1"},
{"Dog","dogname2"},{"Cat","catname1"}};

public IList setAnimals()
{
IList animalList = new ArrayList();

// Somehow dynamically create a new instance of an object and
add it to the animalList
// e.g. animals[0,0] is a Dog type, therefore dynamically
create a Dog class, and set the name
// for the just created Dog class taken from animals[0,1]
"dogname1"

return animalList;
}
}
}

Is what I''m trying to do possible in C#? Does anyone have any ideas
how this can be done?

Thanks,
Jerry

推荐答案

我认为可以从COM源找到解决问题的最佳方案。


创建一个可以产生对象。

类的一个方法是类的类型名称字符串,

,方法内部包含if-else-if-else ... whick返回

对应的对象。


喜欢这个:


Class Spawn

{

public System.Object getInstance(string typeName)

{

if(typeName ==" Dog")

返回新的狗();

否则如果/ *等等* /

}

}

jerryau写道:
I think the best solution for your problem can be found from the COM source.

Create a class which can spawn objects.
One method of the class take the type namestring for the class,
and inside the method contains an if-else-if-else... whick returns the
corresponding object.

Like this:

Class Spawn
{
public System.Object getInstance(string typeName)
{
if(typeName == "Dog")
return new Dog();
else if /* and so on */
}
}
jerryau wrote:




我试图根据字符串动态创建对象实例

类名,然后我需要动态设置值这些

对象,但我不知道如何在C#中执行此操作。以下是我需要做的例子




****** Dog.cs ******

命名空间MySystem

{

公共类狗

{

私人字符串名称;


公共字符串名称

{

get {return name; }

set {name = value; }

}

}

}


****** Cat.cs * *****

命名空间MySystem

{

公共类Cat

{

私人字符串名称;


公共字符串名称

{

get {return name; }

set {name = value; }

}

}

}


****** SetAnimals.cs * *****

命名空间MySystem

{

公共类SetAnimals

{

string [,] animals = new string [,] {{" Dog"," dogname1"},

{" Dog"," dogname2"},{" Cat" ;,catname1}};


公共IList setAnimals()

{

IList animalList = new ArrayList() ;


//以某种方式动态创建一个对象的新实例,并且

将它添加到animalList

//例如animals [0,0]是一种Dog类型,因此动态地创建一个Dog类,并为刚从动物身上创建的Dog类设置名称

// [ 0,1]

" dogname1"


返回animalList;

}

} < br $>
}


我想在C#中做些什么?有没有人有任何想法

如何做到这一点?


谢谢,

Jerry
Hi,

I am trying to dynamically create object instances based on a string
class name, and then I need to dynamically set values to these
objects, but I have no idea how to do this in C#. Here is the example
of what I need to do:

******Dog.cs******
namespace MySystem
{
public class Dog
{
private string name;

public string name
{
get { return name; }
set { name = value; }
}
}
}

******Cat.cs******
namespace MySystem
{
public class Cat
{
private string name;

public string name
{
get { return name; }
set { name = value; }
}
}
}

******SetAnimals.cs******
namespace MySystem
{
public class SetAnimals
{
string[,] animals = new string[,] {{"Dog","dogname1"},
{"Dog","dogname2"},{"Cat","catname1"}};

public IList setAnimals()
{
IList animalList = new ArrayList();

// Somehow dynamically create a new instance of an object and
add it to the animalList
// e.g. animals[0,0] is a Dog type, therefore dynamically
create a Dog class, and set the name
// for the just created Dog class taken from animals[0,1]
"dogname1"

return animalList;
}
}
}

Is what I''m trying to do possible in C#? Does anyone have any ideas
how this can be done?

Thanks,
Jerry


或者你可以切换一个类的HashCode。

那可能会更快。:)


jerryau写道:
Or you can switch a class''s HashCode.
That may be faster.:)

jerryau wrote:




我试图根据字符串动态创建对象实例

类名,然后我需要动态设置这些

对象的值,但我不知道如何在C#中执行此操作。以下是我需要做的例子




****** Dog.cs ******

命名空间MySystem

{

公共类狗

{

私人字符串名称;


公共字符串名称

{

get {return name; }

set {name = value; }

}

}

}


****** Cat.cs * *****

命名空间MySystem

{

公共类Cat

{

私人字符串名称;


公共字符串名称

{

get {return name; }

set {name = value; }

}

}

}


****** SetAnimals.cs * *****

命名空间MySystem

{

公共类SetAnimals

{

string [,] animals = new string [,] {{" Dog"," dogname1"},

{" Dog"," dogname2"},{" Cat" ;,catname1}};


公共IList setAnimals()

{

IList animalList = new ArrayList() ;


//以某种方式动态创建一个对象的新实例,并且

将它添加到animalList

//例如animals [0,0]是一种Dog类型,因此动态地创建一个Dog类,并为刚从动物身上创建的Dog类设置名称

// [ 0,1]

" dogname1"


返回animalList;

}

} < br $>
}


我想在C#中做些什么?有没有人有任何想法

如何做到这一点?


谢谢,

Jerry
Hi,

I am trying to dynamically create object instances based on a string
class name, and then I need to dynamically set values to these
objects, but I have no idea how to do this in C#. Here is the example
of what I need to do:

******Dog.cs******
namespace MySystem
{
public class Dog
{
private string name;

public string name
{
get { return name; }
set { name = value; }
}
}
}

******Cat.cs******
namespace MySystem
{
public class Cat
{
private string name;

public string name
{
get { return name; }
set { name = value; }
}
}
}

******SetAnimals.cs******
namespace MySystem
{
public class SetAnimals
{
string[,] animals = new string[,] {{"Dog","dogname1"},
{"Dog","dogname2"},{"Cat","catname1"}};

public IList setAnimals()
{
IList animalList = new ArrayList();

// Somehow dynamically create a new instance of an object and
add it to the animalList
// e.g. animals[0,0] is a Dog type, therefore dynamically
create a Dog class, and set the name
// for the just created Dog class taken from animals[0,1]
"dogname1"

return animalList;
}
}
}

Is what I''m trying to do possible in C#? Does anyone have any ideas
how this can be done?

Thanks,
Jerry


4月24日下午1:15,jerryau< jerrya ... @ gmail.comwrote:
On Apr 24, 1:15 pm, jerryau <jerrya...@gmail.comwrote:

我是试图动态创建基于字符串

类名的对象实例,然后我需要动态设置这些

对象的值,但我不知道如何做到这一点在C#中。以下是我需要做的示例


I am trying to dynamically create object instances based on a string
class name, and then I need to dynamically set values to these
objects, but I have no idea how to do this in C#. Here is the example
of what I need to do:



< snip>

<snip>


我正在尝试用C#做什么?有没有人有任何想法

如何做到这一点?
Is what I''m trying to do possible in C#? Does anyone have any ideas
how this can be done?



听起来像是在Spring.NET之后:
http://springframework.net

Jon

Sounds like you''re after Spring.NET:
http://springframework.net

Jon


这篇关于动态bean实例化(反射???)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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