0

I am guessing this isn't possible since c# doesn't support prototyping but worth an ask.

I have a big set of unrelated classes, all inheritable, let's take two of them: Bar and Baz.

I want to extend all these classes in the same way: adding two public int properties: X and Y, and potentially some other private variables and methods down the road. Let's call this class (before inheriting) Foo, and Foo is meant to inherit Baz, Bar and so on.

I don't want to have to explicitly create different classes for Bar, Baz, ....

class FooBaz : Baz

class FooBar : Bar

...

Instead, Is there a way I can create a kind of factory that takes the classes, e.g bar, baz and then returns an object of the that is foo : bar, or foo : baz etc?

Var FooedBaz = New Foo(Baz);

WriteLine(FooedBaz.X);

Why do I want this?

Because I am using WPF and extending various UIElements like Image to add x and y coordinates onto them. This way I can directly use the classes within the WPF system without have to access members etc.

12
  • 3
    No, but you could use composition for this really easily, where Foo<T> has an instance of T, as well as the X and Y properties you want. Unfortunately we don't know why you're currently aiming for inheritance, so we can't tell whether composition is a valid alternative. Commented Feb 15, 2023 at 16:00
  • 3
    Maybe you could provide a little more detail about what your actual requirements are? I can't imagine a scenario where this would be needed where, say, generics couldn't accommodate your needs. Commented Feb 15, 2023 at 16:01
  • Have updates the post with more detail. Yes I realize I could use generics, but I was wondering if there is a cleaner solution. Commented Feb 15, 2023 at 16:05
  • 1
    Oh okay, well now seeing your concrete case, I'd recommend you take a look at the Visitor pattern. Commented Feb 15, 2023 at 16:06
  • 1
    You could use a dependency object and attached dependency properties and build a dependency object (class) with a variable set of properties. You could then inject delegates such as actions for variable methods. Commented Feb 15, 2023 at 17:09

1 Answer 1

1

If having "real" inheritance is not a mandatory requirement, I would go with ICustomTypeDescriptor, or with CustomTypeDescriptor which has most of the boilerplate pre-implemented, if you do not have change it. Thus it will be working effortlessly with all of the .NET Type APIs. The implementation can receive an instance of an arbitrary type, decorate it with the additional logic, and then route this logic through the ICustomTypeDescriptor API. Here is a very dummy pseudo implementation.

    private class DynamicType<T> : UIElement, ICustomTypeDescriptor where T : UIElement
    {
        public DynamicType(T wrapped)
        {
          . . .
        }

        public int CustomProp1 { get; set; }

        public int CustomProp2 { get; set; }

        public override PropertyDescriptorCollection GetProperties()
        {
            var prop1AndProp2 = base.GetProperties();
            var wrappedProps =  TypeDescriptor.GetProperties(wrapped);
            return prop1AndProp2 + wrappedProps;
        }
        
        . . . other ICTD methods . . .
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.