从自己的 superclass 中在 class 中进行 C# 类型转换

C# type casting in class from own superclass

下面是我从一个项目的源代码中写出来的代码。我通过反编译器获得了这些代码。当我正常安装时程序运行良好但是当我编译源代码时,它抛出下面的异常。

这可能吗;

public abstract class ComPortSettingsBase
{
}

public class ComportSettings: ComPortSettingsBase
{
}

public abstract class Comport
{
    public ComPortSettingsBase Settings
    {
        get
        {
            return this.settings;
        }
        set
        {
            this.settings = value;
        }
    }
}

public class ComportSetup: Comport
{
    public ComportSettings Settings
    {
        get
        {
           return (ComPortSettings)base.Settings
        }
        set
        {
           // The problem is here.
           // Firts of all type casting is not valid. It causes type casting exception
           // If I remove the type casting it causes Whosebug exception normally. 

           this.Settings = (ComPortSettingsBase)value;
        }
    }
}

试试这个

public class ComportSetup: Comport
{
    public ComportSettings Settings
    {
        get
        {
           return (ComportSettingsBase)base.Settings
        }
        set
        {
           base.Settings = (ComPortSettingsBase)value;
        }

您似乎遗漏了 Comport class 中的 private ComportSettingsBase settings;

此处例外较少:

    public abstract class ComPortSettingsBase
{
}

public class ComportSettings : ComPortSettingsBase
{
}

public abstract class Comport
{
    private ComPortSettingsBase settings;
    public ComPortSettingsBase Settings
    {
        get
        {
            return this.settings;
        }
        set
        {
            this.settings = value;
        }
    }
}

public class ComportSetup : Comport
{
    public ComportSettings Settings
    {
        get
        {
            //here the cast is redundant since base.Settings is of type ComPortSettingsBase
            var casted = (ComPortSettingsBase) base.Settings;
            //but this is illegal since you try to return something of type ComPortSettingsBase and tell that it's of type ComportSettings
            return casted;
        }
        set
        {
            //please note that you are casting value to ComPortSettingsBase and this is legal since ComportSettings extends ComPortSettingsBase
            var casted = (ComPortSettingsBase)value;
            //but here you try to assing ComPortSettingsBase to a ComportSettings property, that is illegal
            this.Settings = casted;
        }
    }
}

我认为是编译前的代码。

public abstract class ComPortSettingsBase
{
}

public class ComportSettings : ComPortSettingsBase
{
}

public abstract class Comport
{
    private ComPortSettingsBase settings;
    public ComPortSettingsBase Settings
    {

        get
        {
            return this.settings;
        }
        set
        {
            this.settings = value;
        }
    }
}

public class ComportSetup : Comport
{
    private ComPortSettingsBase Settings;
    public ComportSettings Settings
    {
        get
    {
        return (ComportSettings)base.Settings;
    }
        set
        {
            // The problem is here.
            // Firts of all type casting is not valid. It causes type casting exception
            // If I remove the type casting it causes Whosebug exception normally. 

            base.Settings = (ComPortSettingsBase)value;
        }
    }
}