你有这些代码的替代品,我想要一个更通用的代码我尝试转换类但没有成功
public object convert(Type type, string value)
{
object r = null;
if (type == typeof(bool))
{
r = bool.Parse(value);
}
else if (type == typeof(int))
{
r = int.Parse(value);
}
else if (type == typeof(string))
{
r = value;
}
return r;
}
Run Code Online (Sandbox Code Playgroud) 我想访问基本classe中的派生类成员:
class Program
{
static void Main(string[] args)
{
B b = new B();
b.FieldTest = 5;
b.MethodeTest();
}
}
public class A
{
public void MethodeTest()
{
//will return B
Type t = this.GetType();
Console.WriteLine(t);
var temp = ???.FieldTest;
//i want that these return 5
Console.WriteLine(temp);
Console.ReadLine();
}
}
public class B:A
{
public int FieldTest;
}
Run Code Online (Sandbox Code Playgroud)
我不确定这些是可能的,但我希望你有任何想法来解决它.
谢谢