小编Sai*_*aid的帖子

将字符串转换为类型实例

你有这些代码的替代品,我想要一个更通用的代码我尝试转换类但没有成功

        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)

c# type-conversion

3
推荐指数
2
解决办法
437
查看次数

访问基本classe中的派生类成员

我想访问基本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)

我不确定这些是可能的,但我希望你有任何想法来解决它.

谢谢

c# inheritance

0
推荐指数
1
解决办法
160
查看次数

标签 统计

c# ×2

inheritance ×1

type-conversion ×1