小编MGG*_*GGD的帖子

将父级转换为其通用子级

我有以下设置:

public class Child<T>: BaseClass
{
    public T Value;
}

public class IntChild: Child<int> { }

public class BoolChild: Child<bool> { }

public class FloatChild: Child<float> { }

public class MyProgram
{
    public BaseClass Source;

    public void SetValue(object val)
    {
        // I want to do something like the following
        // ((Child) Source).Value = (val.GetType()) val;

        // Instead, I have to do it like this
        string temp = val.ToString();
        switch (Source.GetType())  
        {
            case "IntChild":
                ((IntChild) Source).Value = int.Parse(val.ToString());
                break;
            case "BoolChild": …
Run Code Online (Sandbox Code Playgroud)

c# unity-game-engine

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

标签 统计

c# ×1

unity-game-engine ×1