当前位置: 首页 > 编程语言 > C#

如何将对象转换为传递给函数的类型?分享

时间:2023-04-10 17:28:28 C#

如何将对象转换为传递给函数的类型?这不会编译,但我想做的就是将对象转换为传递给函数的“t”?publicvoidMy_Func(Objectinput,Typet){(t)objectab=TypeDescriptor.GetConverter(t).ConvertFromString(input.ToString());你可以这样做:objectab=Convert.Changetype(input,t);但是,貌似要以强类型的方式使用ab,只能通过泛型来实现:以上是C#学习教程:Howtoconvertanobjecttothetypepassedtothefunction?如果分享的内容对你有用,需要了解更多C#学习教程,希望你多多关注——publicvoidMy_Func(Objectinput){Tab=(T)Convert.ChangeType(input,类型(T));}publicvoidMy_Func(Objectinput,Typet){objecttest=newobject();test=Convert.ChangeType(test,t);test=TypeDescriptor.GetConverter(t).ConvertFromString(input.ToString());}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: