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;
}
}
}
As you can see its primary use is for WPF Resources like Converts, where you normally declare a key in XAML thats static to get this instance also for Codebehind Binding Creation.
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.
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, 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.