1

the easiest way to describe my problem is with example code. I know that this won't compile, but i need a similar option

abstract class Foo 
{
protected abstract static  ElementName {get;} 
}
class Bar : Foo
{
protected static override ElementName
{
    get
    {
        return "bar";
    }
}
}
class Baz<T> where T : Foo
{
public string ElementName 
{
    get
    {
        return T.ElementName;
    }
}
}

Grettings

  • Possible duplicate of [Is there a way to force a C# class to implement certain static functions?](http://stackoverflow.com/questions/577749/is-there-a-way-to-force-a-c-sharp-class-to-implement-certain-static-functions) – Dark Falcon Jun 11 '16 at 15:56

1 Answers1

2

This cannot be done in the way you want, but you can achieve something similar using reflection. Here is an example offering two possible solutions to your problem (updated):

abstract class Foo
{
    protected abstract string _ElementName { get; }

    public static string GetElementName<T>() where T : Foo, new()
    {
        return typeof(T).GetProperty("_ElementName", BindingFlags.Instance | BindingFlags.NonPublic)?
                        .GetValue(new T()) as string;
    }

    public static string GetStaticElementName<T>() where T : Foo, new()
    {
        return typeof(T).GetProperty("ElementName", BindingFlags.Static | BindingFlags.NonPublic)?
                        .GetValue(null) as string;
    }
}

class Bar : Foo
{
    protected static string ElementName
    {
        get
        {
            return "StaticBar";
        }
    }

    protected override string _ElementName
    {
        get
        {
            return "Bar";
        }
    }
}

class FooBar : Bar
{
    protected static string ElementName
    {
        get
        {
            return "StaticFooBar";
        }
    }

    protected override string _ElementName
    {
        get
        {
            return "FooBar";
        }
    }
}

class Baz<T> where T : Foo, new()
{
    public string ElementName
    {
        get
        {
            return Foo.GetElementName<T>();
        }
    }

    public string StaticElementName
    {
        get
        {
            return Foo.GetStaticElementName<T>();
        }
    }
}

...

Console.WriteLine(new Baz<Bar>().ElementName); // Bar
Console.WriteLine(new Baz<FooBar>().ElementName); // FooBar
Console.WriteLine(new Baz<Bar>().StaticElementName); // StaticBar
Console.WriteLine(new Baz<FooBar>().StaticElementName); // StaticFooBar
NValchev
  • 2,855
  • 2
  • 15
  • 17
  • Thank you, that should be solution. But the GetProperty finds non value case is not handled –  Jun 11 '16 at 18:16
  • what do you mean, can you give an example? – NValchev Jun 12 '16 at 07:16
  • If GetProperty called, but there is no property, it will return null. The GetValue method will now raise an NullReferenceException, does it? –  Jun 12 '16 at 07:20
  • Yes, it will. Add [null-conditional operator](https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6#null-conditional-operators) - `?` behind the `GetProperty` method, this way if it returns `null`, the chained method would not be called at all – NValchev Jun 12 '16 at 07:27