Why I’m unable to extend an abstract class. Is there any work around to achieve this?
In silverlight, Enum.GetNames is missing. So, I would like to extend it and have it in my utility assembly. By then, got into this.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The problem here is not that you can’t add an extension method to an abstract class (you can – you can add an extension method to any type) – it’s that you can’t add a static method to a type with extension methods.
Extension methods are static methods that present themselves in C# as instance methods. But they’re still static. Adding a static method to a type requires the ability to redefine the type, which you can only do if you have the source code 🙂
Best bet, if you want this method, is to write your own static and see if you can perhaps rip the code out of reflector.
However, it’s entirely possible that it’s not there because it’s physically not supported in Silverlight (I don’t know – I haven’t investigate)
EDIT
Following on from your comment – and I hope that I’ve understood you here – I think what you want to be able to do is something like this (targetting
objectto prove the point):If you think about it – of course you can’t inject new static methods into an existing type because, like I said in paragraph two, you have to be able to recompile the underlying type; and there simply is no way around that.
What you can do is (and I don’t actually recommend this – but it’s an option) create yourself a new type called
Enumand place it inside a new namespace:And now – when you want to use it, what you can’t do is this:
Because the using in the outermost scope to both ‘System’ and ‘MySystem’ will cause the compiler not to be able to resolve the correct
Enumtype.What you can do, however, is this:
Now, code within that namespace (within that file only) will always resolve
Enumto the one in your namespace because that’s the nearestusingdirective in terms of scope.So, you can think of this as overriding the whole
Enumtype for the benefit of a given namespace that includes ausing MySystem;in it.But, it does exactly that – it replaces the existing
System.EnumwithMySystem.Enum– meaning that you lose all the members of theSystem.Enumtype.You could get around this by writing wrapper methods in your
Enumtype around theSystem.Enumversions – making sure that you fully-qualify the type asSystem.Enum.Having looked at the implementation of the GetNames method in Reflector – it relies on internal data that I don’t think you’re going to be able to build… but I would be very interested to hear if you are actually able to reproduce the method in Silverlight.