How inheritance can be applied to a class group once

In C #, I would like an abstract class to apply to many other classes. How can I do this without labeling each class.

public abstract class Bar
{
 public bool Blah { get; set; }
}

public class Foo : Bar
{
 public int FooId { get; set; }
}

public class Stool : Bar {}
public class Fun : Bar {}
public class NoFun : Bar {}

etc. etc.

Is there a way to just grab each class and then mark it as inheritance Bar?

+3
source share
2 answers

No. You may have a visual studio add-on that did this, or some other similar tool for static code manipulation, but from the point of view of the language itself there is no way to change type inheritance at runtime, and as far as I don’t know about the existing visual studio functions for this .

+4
source

/ VS, , Reflection.Emit, , , . , .

, . , :

    AssemblyName assemblyName = new AssemblyName("MyDynamicAssembly");

    AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly
        (assemblyName, AssemblyBuilderAccess.RunAndSave);

    ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule
        ("MyDynamicAssembly", "MyDynamicAssembly.dll");

    TypeBuilder typeBuilder = moduleBuilder.DefineType
        ("MyDynamicAssembly." + typeName, TypeAttributes.Public, typeof(object));

    typeBuilder.AddInterfaceImplementation(typeof(IMyInterface)); 

    typeBuilder.DefineDefaultConstructor(MethodAttributes.Public);
+1

All Articles