静态实例基类/派生类 [英] Static Instance Base/Derived class

查看:83
本文介绍了静态实例基类/派生类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在基类中编写一个静态实例属性并将其派生,但是我遇到了一些问题。

I would like to write a static instance property in a base class and derive this, but I am facing some problems.

这是基类的代码-我目前有:

Here is the code for the base class - I currently have:

public abstract class ResourceInstance<T>
{
    private static T _instance;
    public static T Instance
    {
        get
        {
            if (_instance != null)
                return _instance;

            var method = MethodBase.GetCurrentMethod();
            var declaringType = method.DeclaringType;
            if (declaringType != null)
            {
                var name = declaringType.Name;
                _instance = (T)Application.Current.TryFindResource(name);
            }

            return _instance;
        }
    }
}

您可以看到其主要内容

使用它用于WPF之类的WPF资源,如Converts,通常在XAML中声明一个静态的键,以便将该实例也用于Codebehind Binding Creation。只需编写以获取XAML中声明的资源即可。

With this it should be possible to just write to get the resource declared in XAML:

var fooConverter = FooConverter.Instance;

现在,这在基类中很正常。

Now this works fine in the base class obviosly.


  1. MethodBase.GetCurrentMethod()。DeclaringType.Name将
    始终返回 ResourceInstance,我希望得到派生的
    类名,因为在我们的应用程序中,ClassName == ResourceKey

  1. the MethodBase.GetCurrentMethod().DeclaringType.Name will always return "ResourceInstance", and I hoped to get the derived class name, since in our Application the ClassName == ResourceKey

Resharper,总是抱怨我正在快速地从派生类访问
静态属性,并且想要我可以通过基类访问它

Resharper, always complain about the fast that I am accessing a static property from the derived class and wants me to access it through the base class

以下是派生类的一个示例:

Here is an example of a derived class:

public abstract class BaseConverter : ResourceInstance<IValueConverter>, IValueConverter
{
    public virtual object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }

    public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }
}

public class FooConverter : BaseConverter
{
    public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return true;
    }
}

希望能为您提供帮助,

Hope you can help, thx.

推荐答案

不确定我是否正确理解了您的问题,但我会这样做:

Not sure if I understand your question correctly, but I would do it like this:

public class ResourceInstance<T>
    where T : ResourceInstance<T> // To ensure correct usage
{
    private static T _instance;
    public static T Instance
    {
        get
        {
            if (_instance != null)
                return _instance;

            var name = typeof(T).Name;
            _instance = (T)Application.Current.TryFindResource(name);

            return _instance;
        }
    }
}

public class FooConverter : ResourceInstance<FooConverter>, IValueConverter
{
    ...
}

对T的约束确保该类将与 class模式一起使用X:ResourceInstance< X> ;这样, typeof(X)将始终为 X

The constraint on T ensures that the class will be used with the pattern class X : ResourceInstance<X>; this way, typeof(X) will always be X.

这篇关于静态实例基类/派生类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆