动态bean实例化(反射???)

编程入门 行业动态 更新时间:2024-10-23 08:23:51
本文介绍了动态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; } } } 我想在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; } } } 我想在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; } } } 我想在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 ... @ gmailwrote: On Apr 24, 1:15 pm, jerryau <jerrya...@gmailwrote: 我是试图动态创建基于字符串 类名的对象实例,然后我需要动态设置这些 对象的值,但我不知道如何做到这一点在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之后: springframework Jon

Sounds like you''re after Spring.NET: springframework Jon

更多推荐

动态bean实例化(反射???)

本文发布于:2023-08-04 17:36:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1298279.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:反射   实例   动态   bean

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!