如何将字符串类型转换为用户定义的自定义类型

Pra*_*eep -1 c# string parsing

我有一个字符串值,需要将其转换为用户定义的自定义类型。怎么做,请帮帮我。

public class ItemMaster
{
    public static ItemMaster loadFromReader(string oReader)
    {
        return oReader;//here i am unable to convert into ItemMaster type
    }
}
Run Code Online (Sandbox Code Playgroud)

jjn*_*guy 5

根据您的类型,有两种方法可以执行此操作。

首先是将构造函数添加到带有String参数的类型中。

public YourCustomType(string data) {
    // use data to populate the fields of your object
}
Run Code Online (Sandbox Code Playgroud)

第二个是添加静态Parse方法。

public static YourCustomType Parse(string input) {
    // parse the string into the parameters you need
    return new YourCustomType(some, parameters);
}
Run Code Online (Sandbox Code Playgroud)