简单定义一个myclass类:public class myclass
{public string a1;//myclass类内定义一个接受Object参数的mymethod方法
public void mymethod(Object class_model)
{ Type mytype=class1.GetType(); //获取class1的类型
string temp=(mytype)class_model.a1; //把已经转为Object类型的对象重新转回mytype类型(即class1的类型myclass) Console.Write(temp); }}程序中如下调用:myclass class1=new myclass();myclass class2=new myclass(); //实例化两个myclass类class2.a1="100"; //给class2的成员赋值class1.mymethod(class2); //执行mythod方法错误发生在mymethod里面,发现mytype并不能取代实际的类型标识来转换对象类型,比如string temp=(mytype)class_model.a1;
不能用mytype代替myclass,正确的转换如下:
string temp=(myclass)class_model.a1;
现在为达到某些目的(主要是通用性),我不想把程序写死,在mymethod中不能使用具体的类型来转换。请问各位,有良方可支吗?恭听!
{public string a1;//myclass类内定义一个接受Object参数的mymethod方法
public void mymethod(Object class_model)
{ Type mytype=class1.GetType(); //获取class1的类型
string temp=(mytype)class_model.a1; //把已经转为Object类型的对象重新转回mytype类型(即class1的类型myclass) Console.Write(temp); }}程序中如下调用:myclass class1=new myclass();myclass class2=new myclass(); //实例化两个myclass类class2.a1="100"; //给class2的成员赋值class1.mymethod(class2); //执行mythod方法错误发生在mymethod里面,发现mytype并不能取代实际的类型标识来转换对象类型,比如string temp=(mytype)class_model.a1;
不能用mytype代替myclass,正确的转换如下:
string temp=(myclass)class_model.a1;
现在为达到某些目的(主要是通用性),我不想把程序写死,在mymethod中不能使用具体的类型来转换。请问各位,有良方可支吗?恭听!
http://community.csdn.net/Expert/TopicView.asp?id=3707994然后把问题说得更具体.
class myclass = (class)Activator.CreateInstance(mytype);
比如我们编制了一个Employee的类,并
Object o1=new Employee();//实例化Employee的对象e1,并转型为Object然后可以再转型为Employee
Employee e1=(Employee)o1; //注意这行,这里需要显式地指定Employee这是传统的类型转换方法。现在我要实现动态转换类型,比如我还可能有一个Boss类,转型的时候还得显式指定Boss,如下:
Boss e1=(Boss)o1; //注意这行,这里需要显式地指定Boss这样就不大灵活,那么我设想生成一个函数动态进行类型转换,比如定义一个包含ConvertMethod(Object model)方法的基类,然后让Employee类与Boss类都继承自这个基类,那么这两个类都获得ConvertMethod方法的使用权。
ConvertMethod定义如下:
public void ConvertMethod(Object model)
{ Type mytype=this.GetType(); //利用反射获取当前对象的类型,可区分为当前为Employee类还是Boss类
mytype temp=(mytype)model; //把Object类型的model对象重新转回mytype类型 }
那么我在程序中调动:Employee e1= new Employee();
Employee e2=new Employee();e1.ConvertMethod(e2); //对e2进行转换操作
Boss b1=new Boss();
Boss b2=new Boss();b1.ConvertMethod(b2); //对b2进行转换操作大家应该可以看到这样实现的好处了吧,即ConvertMethod能实现“动态转换”。遗憾的是我写的这个ConvertMethod方法不成功,Type mytype=this.GetType()定义的mytype并不能代替实现的类型(如Employee或Boss)来进行类型转换。大家可以想个行得通的方法吗??致谢!
如果你真的要转的话应该需要设定个boss类的父接口
Type mytype=this.GetType();//这个this是指当前调用ConvertMetohd方法的对象e1,e2是同一类型的不同对象,在进行e1.ConvertMethod(e2)的操作时,
mytype获得的类型是跟e1相关的,所以使用mytype调用的方法属性均只能是e1的成员方法、成员变量
谢谢,愿与各位在线讨论!
int b=0;
Console.WriteLine(b.GetType());//显示System.Int32
object c=Convert.ChangeType(b,a.GetType());
Console.WriteLine(c.GetType());//显示System.String
说白了你的要求是能根据Type来实现转换,类似vb的CType()是不是?
以上代码不知道对你有没有启发
你可以使用接口和父类解决上面的问题。public class a
{
public int ID;
}public class b:a
{
}public class c:a
{
}
object aa = new b();int id = ((a)aa).ID;
{
int ID{get;set;}
}public class b:ia
{
int id;
public int ID
{
get
{
return id;
}
set
{
id = value;
}
}
}public class c:ia
{
int id;
public int ID
{
get
{
return id;
}
set
{
id = value;
}}
object aa = new b();int id = ((ia)aa).ID;
CodeCompileUnit CompileUnit = new CodeCompileUnit();
CodeNamespace Samples = new CodeNamespace("Samples");
Samples.Imports.Add( new CodeNamespaceImport("System") );
CompileUnit.Namespaces.Add( Samples );
CodeTypeDeclaration Class1 = new CodeTypeDeclaration("Class1");
Samples.Types.Add(Class1);
CodeMemberMethod method1 = new CodeMemberMethod();
method1.Name = "ReturnString";
method1.ReturnType = new CodeTypeReference(mytype.TypeName);
method1.Parameters.Add( new CodeParameterDeclarationExpression("Object", "text") );
method1.Statements.Add( new CodeMethodReturnStatement( new CodeArgumentReferenceExpression("text") ) );
Class1.Members.Add( Start );
CSharpCodeProvider provider = new CSharpCodeProvider();ICodeCompiler compiler = provider.CreateCompiler();
CompilerParameters cp = new CompilerParameters(new string[] {"System.dll"}, filepath.Substring(0, filepath.LastIndexOf(".")+1)+"exe", false);CompilerResults cr = compiler.CompileAssemblyFromDom(CompileUnit, filepath);
编译成程序集,引用此程序集的start方法。
其中写得不全和错的地方,自己改改。只是一个思路。
Object obj=Activator.CreateInstance(t);
int id = ((a)aa).ID;
的确可以实现,但我想探讨的是通用性的转型,比如我考虑这样实现:
int id = ((*)aa).ID; //*号表示转换的一个“通配符”这样的效果这个*从哪里得到呢?就是从aa类中得到的类型,比如:
*=aa.GetType();这个问题好象很难表述清楚,哎,也不知本人的设想是否现实
Type t=src_ctl.GetType();
//创建class类型的object
Object obj=Activator.CreateInstance(t);
//属性名字
String strSrcPdcName = src_pdc[i].Name;
//取得属性值
object objSrcPdcValue = src_pdc[i].GetValue(src_ctl);
//设置属性值
dst_pdc[strSrcPdcName].SetValue(dst_ctl, objSrcPdcValue);
各位同志,我怎么越看越像是泛型的问题。.NET1.1好象还不支持泛型吧唉,希望的泡泡又破了!!!!!