本文介绍了如何将字符串类型转换为用户定义的自定义类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串值,需要将其转换为用户定义的自定义类型.如何做到这一点,请帮助我.
I have a string value that needs to be converted into my user defined custom type. how to do this, please help me.
public class ItemMaster
{
public static ItemMaster loadFromReader(string oReader)
{
return oReader;//here i am unable to convert into ItemMaster type
}
}
推荐答案
根据您的类型,可以采用两种方法.
Depending on your type there are two ways that you could do it.
首先是将类型为String
的构造函数添加到您的类型中.
The first is adding a constructor to your type that takes a String
parameter.
public YourCustomType(string data) {
// use data to populate the fields of your object
}
第二个是添加静态Parse
方法.
The second is adding a static Parse
method.
public static YourCustomType Parse(string input) {
// parse the string into the parameters you need
return new YourCustomType(some, parameters);
}
这篇关于如何将字符串类型转换为用户定义的自定义类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!