0

当我尝试将我的单例类加载到 PowerShell 中时,我遇到了一些麻烦。具体来说,当我尝试访问实例时出现以下错误:

You cannot call a method on a null-valued expression.
At C:\Users\*Omitted*\Documents\visual studio
2012\Projects\ClassLibrary1\ClassLibrary1\bin\Debug\test.ps1:5 char:1
+ $test = [ClassLibrary1.Class1]::Instance.Foo()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

当我运行这个示例脚本时:

$currentScriptDirectory = Get-Location
[System.IO.Directory]::SetCurrentDirectory($currentScriptDirectory.Path)
[Reflection.Assembly]::LoadFrom("ClassLibrary1.dll") | fl
[ClassLibrary1.Class1] | Get-Member -Static
$test = [ClassLibrary1.Class1]::Instance.Foo()

在这个简单的类上:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary1
{
    public class Class1
    {
        private static Class1 instance = null;

        private Class1()
        {

        }

        public static Class1 Instance
        {
            get
            {
                if (null == instance)
                {
                    instance = new Class1();
                }

                return instance;
            }
        }

        public string Foo()
        {
            return "HI THERE";
        }
    }
}

关于如何在不改变单例类的情况下摆脱这个错误的任何想法?我继承了我正在使用的类并且不能改变它的架构。

4

1 回答 1

0

主要问题是您对静态和实例感到困惑。静态意味着它是类级别的,例如实例。该Foo方法应该是实例,因为这似乎是您想要的。这是固定代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary1
{
    public class Class1
    {
        private static Class1 instance = null;
        private Class1()
        {
        }
        public static Class1 Instance
        {
            get
            {
                if (null == instance)
                {
                    instance = new Class1();
                }

                return instance;
            }
        }
        public string Foo()
        {
            return "HI THERE";
        }
    }
}

然而,这不是一个防弹的单例模式。Jon Skeet 做了一些不错的,在这里。

这是它的正确实现:

public sealed class Class1
{
    private static readonly Lazy<Class1> lazy =
        new Lazy<Class1>(() => new Class1());
    public static Class1 Instance { get { return lazy.Value; } }
    private Class1()
    {
    }
    public string Foo()
    {
        return "HI THERE";
    }
}
于 2013-07-01T18:26:44.297 回答