Search code examples
c#clrfluxfody

Need workaround for restriction of virtual, abstract static members for .NET implementation of Facebook Flux


I've looked at a number of Flux implementations in .NET, and think that this approach is pretty swell:

  • It uses static members instead of enums, making XAML nicer
  • No need to convert to/from enum types (cleaner code)
  • Easy integration with almost any pub/sub mechanism.

That being said, I want to keep the static property-to-string coding technique, but I can't make the following work:

public class BitnameTypesBase
{
   // I want to override this in base classes.. but can't 
    public static string ChainName;

    public static string GET_BITNAME = "get_bitname_" + ChainName;
    public static string SEND_BITNAME = "send_bitname_" + ChainName;
    public static string QUERY_BITNAME = "query_bitname_" + ChainName;
}

public class BitcoinBitName :  BitnameTypesBase
{
   // Ouch, can't override field or properties 
    override public static string ChainName = "Bitcoin";

    public static string MySpecialAction = "special_bitname_" + ChainName;
}

public class OthercoinBitName :  BitnameTypesBase
{
   // Ouch, can't override field or properties
    override public static string ChainName = "something new";

    public static string MySpecialAction = "special_bitname_" + ChainName;
}

Question

  • Is there something in the language that will permit this?
  • Is there any pre compilation script I can do that will permit this?
  • Is there any Fody Plugin that I can use that could help (I didn't see one)

Solution

  • You can't override a static member, but you can hide them by using the new modifier. For exaple:

    public class BitnameTypesBase
    {
        public static string ChainName;
    
        public static string GET_BITNAME = "get_bitname_" + ChainName;
        public static string SEND_BITNAME = "send_bitname_" + ChainName;
        public static string QUERY_BITNAME = "query_bitname_" + ChainName;
    }
    
    public class BitcoinBitName :  BitnameTypesBase
    {
        public static new string ChainName = "Bitcoin";
        public static string MySpecialAction = "special_bitname_" + ChainName;
    }
    
    public class OthercoinBitName : BitnameTypesBase
    {
        public static new string ChainName = "something new";
        public static string MySpecialAction = "special_bitname_" + ChainName;
    }
    

    And now:

    Console.WriteLine(BitcoinBitName.MySpecialAction);
    Console.WriteLine(OthercoinBitName.MySpecialAction);
    

    Will output:

    special_bitname_Bitcoin
    special_bitname_something new