17

クラスが.netでインスタンス化されないようにする方法を知る必要がありますか?

クラスを抽象および静的にするようないくつかのメソッドを知っています。

これを達成する方法は他にありますか?

4

5 に答える 5

22

インスタンスが絶対に必要ない場合は、クラスを静的にするのが最善の方法です。これにより、誰もインスタンスを作成できなくなります。このクラスは、封印された抽象クラスであり、コンストラクターはありません。

さらに、言語はそれが静的クラスであることを認識し、インスタンスを意味するさまざまな場所(型引数や変数など) での使用を停止します。これは、単にプライベート コンストラクターを使用するよりも明確に意図を示しています。これは、そのクラス内で作成されたインスタンスがあることを意味する可能性があります (たとえば、シングルトン実装用)。

ああ、クラスを静的にすると、クラスに無意味なインスタンスメンバーを導入することもできなくなります:)

静的クラスの詳細については、 MSDNを参照してください。

于 2012-04-13T12:55:07.257 に答える
18

privateコンストラクターをマークするprotectedか、別のアセンブリから使用されている場合は、internal

于 2012-04-13T12:51:46.650 に答える
7

コンストラクターのマーキングprivate。もちろん、これはクラスがstaticメソッドを介してそれ自体をインスタンス化することを妨げません。たとえば...

より実際には、クラスのインスタンス化を禁止する目的は何ですか。singletonを持つ場合は、privateコンストラクターが適切です。サブクラス化を強制する場合は、クラスを作成するabstract方が適切です。ユーティリティメソッドを持つクラスを作成する場合、それを作成するのstaticは1つの方法です(その場合、メソッドのみを持つことができstaticます)。

于 2012-04-13T12:51:20.440 に答える
3

クラスが .net でインスタンス化されないようにする方法を知る必要がありますか?

あなたの質問は明確ではありません。

実行時にインスタンス化されるということですか?クラスabstractまたはを作成しstaticます。

コードでコンストラクターにアクセスできないということですか? コンストラクタを作成しprivateます。ただし、リフレクションを使用してコンストラクターのハンドルを取得し、実行時にインスタンスをインスタンス化する可能性があることに注意してください。

それで、あなたはどちらを意味しますか?

于 2012-04-13T12:54:08.710 に答える
2

If the question is:

How can you make your class not be instanced without having your class be static or abstract?

Then the answer to this is to implement the singleton pattern, which in .NET 4+ this is done easily with:

public sealed class myClass
{
    private static readonly Lazy<myClass> lazyInstance = 
        new Lazy<myClass>(() => new myClass());

    public static Instance
    {
        get
        {
            return lazyInstance.Value;
        }
    }

    private myClass()
    {
        // constructor logic here
    }
}

The singleton pattern allows you to pass your class around as a reference to methods, while still ensuring that you have only a single instance of your class. It also makes testing much easier as you can have a ImyClass instance which myClass implements, this is very helpful when making mock objects.

Without .NET4 you can still implement the singleton pattern, for example:

private static readonly myClass instance = new myClass();

public static Instance
{
    get
    {
        return instance;
    }
}

// rest of code remains the same

Which doesn't have deferred loading until it's called, there's lots of other ways as well (I think about 6 different ways), but the above two are the most common ones.

In summary the question is likely asking if you know the singleton pattern and if you recognise it's importance over static classes for unit tests and mock objects.

As others have already pointed out, static fields, even those marked readonly can be set with reflection, in addition the private constructor can be called using reflection. If you need to prevent these, either you need to make the calling code run in a less trusted app-domain space, or you will need to implement your class as static.

Generally though, people don't bother with such levels of reflection to get around your constraints unless they 'really need to' for example, writing obscure / extreme fringe case unit tests.

于 2012-04-13T20:00:09.573 に答える