0

フレームワークでは、Int32、Double などの多くの値型が参照型、つまり抽象クラス (System.ValueType およびインターン System.Object クラス) から継承されることがわかっています。今、C#コードを介して明示的に同様の概念をシミュレートできるかどうか疑問に思っていました. はい、C# コンパイラのルールが実行をブロックしていることを絶対に知っています。VB.NET もブロックするかどうかわかりませんか?

以下のコードサンプルは、非常に明確に示しています。

    //Error code : Can not be done
    public abstract class MyClass
    {

    }

    public struct MyStruct : MyClass
    {
    }

だから私はilasm.exeを介して同じことをしようとしますが、多くのグーグルのほかに解決できなかった奇妙なエラーが発生しています。私の質問は、フレームワーク内でこの種の継承を行うことは可能ですが、外部では行うことができないということです。

したがって、このシナリオをシミュレートできる方法はありますか?

PS:いいえ、私は現実世界の問題を解決することに関して何も達成していません。私の考えと学びを満足させるだけです。

感謝&ハッピーコーディング、

ゼンウォーカー:)

4

1 に答える 1

2

See ECMA 335 (http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-335.pdf)

Section I.8.9.10 (Value type inheritance):

In their unboxed form value types do not inherit from any type. Boxed value types shall inherit directly from System.ValueType unless they are enumerations, in which case, they shall inherit from System.Enum. Boxed value types shall be sealed.

Logically, the boxed type corresponding to a value type

Is an object type.

Will specify which object type is its base type (i.e., the object type from which it inherits).

Will have a base type that has no fields defined.

Will be sealed to avoid dealing with the complications of value slicing.

The more restrictive rules specified here allow for more efficient implementation without severely compromising functionality.

It's therefore a limitation of the common type system, not of the C# language, that non-enum value types may inherit only from System.ValueType.

于 2012-07-17T06:18:30.237 に答える