0

I have an alias

using SpecialFunction = System.Func<SomeClass, SomeOtherClass, AnotherClass>

I'd like to be able to use this alias in multiple classes without having to rewrite it every time. I'm open to this being a class instead of an alias. Is it possible to do this?

I tried inheriting the type as shown below but as System.Func is sealed this doesn't work.

// This doesn't work as System.Func is sealed public class SpecialFunction : System.Func<SomeClass, SomeOtherClass, AnotherClass> { }

I have read this question about aliasing types and this question about inheriting directives. I don't believe they provide solutions for this case.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
lachy
  • 1,833
  • 3
  • 18
  • 27
  • 2
    Why alias it? Why not make a delegate? – ProgrammingLlama Jul 06 '19 at 03:20
  • 2
    It is unclear what you are actually trying to accomplish. If you really find yourself using a delegate with this signature so often that it's driving you to want to alias the delegate type, why not just declare your own delegate type? Bottom line: you can't inherit sealed classes (all delegate types are sealed), and aliases are per-file only. Short of writing your own preprocessor for your .cs files, you just can't do what you want. You'll be better off explaining the broader issue you're trying to solve and ask for help with that. – Peter Duniho Jul 06 '19 at 04:42
  • @John Ah yup, a delegate was the appropriate solution. Knew they existed but hadn't used them and got caught down this hole rather than checking at a higher level. Thanks! – lachy Jul 07 '19 at 07:14

0 Answers0