mercredi 15 février 2023

How to create a classes that inherit any class at runtime

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.

Aucun commentaire:

Enregistrer un commentaire