转换使用"一般System.String要任何复杂类型; Convert.ChangeType()" [英] Convert System.String generically to any complex type using "Convert.ChangeType()"
问题描述
我尝试一般转换用户输入简单或复杂类型:
类节目
{
静态无效的主要(字串[] args)
{
Console.WriteLine(欢迎光临,请提供以下信息...确认以<返回>!);
Console.WriteLine();
Console.Write(名称(如佩吉苏'));
VAR用户= GetUserInput<使用者>(到Console.ReadLine());
Console.WriteLine();
Console.WriteLine();
Console.WriteLine(嗨{0},很高兴认识你!,user.Forename);
Console.WriteLine();
Console.Write(年龄:);
user.Age = GetUserInput&所述; USHORT>(到Console.ReadLine());
Console.WriteLine();
Console.WriteLine(谢谢,再见!);
Console.WriteLine(新闻<返回>到戒烟......);
到Console.ReadLine();
}
静态牛逼GetUserInput< T>(字符串数据)
{
回报率(T)Convert.ChangeType(数据的typeof(T));
}
}
类用户
{
公众用户(字符串名称)
{
VAR分裂= name.Split ('');
用的名字=分裂[0];
姓氏=分裂[1];
}
公共静态隐运营商的用户(字符串值)
{
返回新用户(值);
}
公共静态明确经营者的字符串(用户值)
{
返回string.Concat(value.Forename,,value.Lastname);
}
公共字符串用的名字{获得;私人集; }
公共字符串姓氏{搞定;私人集; }
公共USHORT年龄{搞定;组; }
}
有关转换我的用户类,我总是得到异常无效的转换,从'System.String'到'ConsoleApplication1.User'。 ?有谁知道如何解决这个问题。
如果我尝试这样的事情(不一般),它只是完美的:
Console.WriteLine((字符串)((用户)佩吉苏));
一个在这里选择可能是同一个类型转换器关联你关心的类型(您可以通过在编译时做到这一点 [TypeConverter的(...)]
,或者出现了,如果你不运行时这样一招。与T类型)
然后是:
的TypeConverter兑换= TypeDescriptor.GetConverter(typeof运算(T)); $ B $(B T)= OBJ(T)conv.ConvertFromString(文本); //或ConvertFromInvariantString
I try to generically convert user input into either simple or complex types:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome, please provide the following info... Confirm with <RETURN>!");
Console.WriteLine();
Console.Write("Name (e.g. 'Peggy Sue'): ");
var user = GetUserInput<User>(Console.ReadLine());
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Hi {0}, nice to meet you!", user.Forename);
Console.WriteLine();
Console.Write("Age: ");
user.Age = GetUserInput<ushort>(Console.ReadLine());
Console.WriteLine();
Console.WriteLine("Thanks and goodbye!");
Console.WriteLine("Press <RETURN> to quit...");
Console.ReadLine();
}
static T GetUserInput<T>(string data)
{
return (T) Convert.ChangeType(data, typeof (T));
}
}
class User
{
public User(string name)
{
var splitted = name.Split(' ');
Forename = splitted[0];
Lastname = splitted[1];
}
public static implicit operator User (string value)
{
return new User(value);
}
public static explicit operator string (User value)
{
return string.Concat(value.Forename, " ", value.Lastname);
}
public string Forename { get; private set; }
public string Lastname { get; private set; }
public ushort Age { get; set; }
}
For the conversion to my "User" class, I always get the exception "Invalid cast from 'System.String' to 'ConsoleApplication1.User'.". Does anyone know how to fix this?
If I try something like this (not generically), it works just perfect:
Console.WriteLine((string) ((User) "Peggy Sue"));
One option here might be to associate a TypeConverter with the types you care about (you can do this at compile-time via [TypeConverter(...)]
, or there is a trick for doing this at runtime if you don't control the types).
Then it is:
TypeConverter conv = TypeDescriptor.GetConverter(typeof(T));
T obj = (T)conv.ConvertFromString(text); // or ConvertFromInvariantString
这篇关于转换使用"一般System.String要任何复杂类型; Convert.ChangeType()"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!