C# 使派生类能够运行父私有方法

C# 使派生类能够运行父私有方法,c#,.net,oop,events,inheritance,C#,.net,Oop,Events,Inheritance,这应该是一个愚蠢的问题,但我被困了很长时间:( 我有一个类,它具有执行某些实体初始化的泛型方法 public class Base { private Base() { // do things // fire OnCreated event } private void InitializeEntity<T>() { // initialize entities of class T }

这应该是一个愚蠢的问题,但我被困了很长时间:(
我有一个类,它具有执行某些实体初始化的泛型方法

public class Base
{
    private Base()
    {
        // do things
        // fire OnCreated event
    }
    private void InitializeEntity<T>()
    {
        // initialize entities of class T
    }
}
也许应该有其他方法来实现这一点。我感谢任何帮助。
更新
伙计们,“这个方法
InitializeEntity
必须在类创建后立即对指定的实体执行。我不想公开(应该说是或保护)这个方法来阻止它在适当的点执行”
。我希望派生类在任何其他时间都不能运行它,但基类
OnCreated
事件除外。我希望它们只能在
OnCreated
事件或类似事件中访问此方法,以便仅在此时导致实体初始化。

改为:

注意,这将允许从同一程序集中的任何类(或该程序集中的
InternalVisibleToAttribute
引用的任何程序集)调用该方法,因此您需要确保正确使用它。

改为:

注意,这将允许从同一程序集中的任何类(或该程序集中的
InternalVisibleToAttribute
引用的任何程序集)调用该方法,因此您需要确保正确使用它。

改为:

注意,这将允许从同一程序集中的任何类(或该程序集中的
InternalVisibleToAttribute
引用的任何程序集)调用该方法,因此您需要确保正确使用它。

改为:


注意:这将允许从同一程序集中的任何类(或该程序集中
InternalVisibleToAttribute
引用的任何程序集)调用该方法,所以由您来确保正确使用它。

如果您不必直接调用
InitializeEntity
,但只想调用它,请将某种标志传递给基本构造函数。要解决
T
问题,可以使用反射

public class Base
{
    protected Base(Type type = null)
    {
        // 1. Base constructor code
        if (type != null)
        {
            // this will to InitializeEntity<type>();
            MethodInfo method = typeof(Base).GetMethod("InitializeEntity", BindingFlags.NonPublic | BindingFlags.Instance);
            MethodInfo generic = method.MakeGenericMethod(type);
            generic.Invoke(this, null);
        }
    }
    private void InitializeEntity<T>()
    {
        // 2. Initialize entities of class T
    }
}

public class Derived : Base
{
    public Derived() : base(typeof(Entity))
    {
        // 3. Derived constructor code
    }
}
公共类基
{
受保护的基(类型=null)
{
//1.基本构造函数代码
if(type!=null)
{
//这将导致初始化属性();
MethodInfo method=typeof(Base).GetMethod(“InitializeEntity”,BindingFlags.NonPublic | BindingFlags.Instance);
MethodInfo generic=method.MakeGenericMethod(类型);
generic.Invoke(this,null);
}
}
私有无效初始值()
{
//2.初始化T类的实体
}
}
派生的公共类:基
{
public-Derived():基(typeof(Entity))
{
//3.派生构造函数代码
}
}

但是,在初始化实体之后执行派生构造函数代码当然有一个限制。

如果您不必直接调用
InitializeEntity
,但只想调用它,请将某种标志传递给基构造函数。要解决
T
问题,可以使用反射

public class Base
{
    protected Base(Type type = null)
    {
        // 1. Base constructor code
        if (type != null)
        {
            // this will to InitializeEntity<type>();
            MethodInfo method = typeof(Base).GetMethod("InitializeEntity", BindingFlags.NonPublic | BindingFlags.Instance);
            MethodInfo generic = method.MakeGenericMethod(type);
            generic.Invoke(this, null);
        }
    }
    private void InitializeEntity<T>()
    {
        // 2. Initialize entities of class T
    }
}

public class Derived : Base
{
    public Derived() : base(typeof(Entity))
    {
        // 3. Derived constructor code
    }
}
公共类基
{
受保护的基(类型=null)
{
//1.基本构造函数代码
if(type!=null)
{
//这将导致初始化属性();
MethodInfo method=typeof(Base).GetMethod(“InitializeEntity”,BindingFlags.NonPublic | BindingFlags.Instance);
MethodInfo generic=method.MakeGenericMethod(类型);
generic.Invoke(this,null);
}
}
私有无效初始值()
{
//2.初始化T类的实体
}
}
派生的公共类:基
{
public-Derived():基(typeof(Entity))
{
//3.派生构造函数代码
}
}

但是,在初始化实体之后执行派生构造函数代码当然有一个限制。

如果您不必直接调用
InitializeEntity
,但只想调用它,请将某种标志传递给基构造函数。要解决
T
问题,可以使用反射

public class Base
{
    protected Base(Type type = null)
    {
        // 1. Base constructor code
        if (type != null)
        {
            // this will to InitializeEntity<type>();
            MethodInfo method = typeof(Base).GetMethod("InitializeEntity", BindingFlags.NonPublic | BindingFlags.Instance);
            MethodInfo generic = method.MakeGenericMethod(type);
            generic.Invoke(this, null);
        }
    }
    private void InitializeEntity<T>()
    {
        // 2. Initialize entities of class T
    }
}

public class Derived : Base
{
    public Derived() : base(typeof(Entity))
    {
        // 3. Derived constructor code
    }
}
公共类基
{
受保护的基(类型=null)
{
//1.基本构造函数代码
if(type!=null)
{
//这将导致初始化属性();
MethodInfo method=typeof(Base).GetMethod(“InitializeEntity”,BindingFlags.NonPublic | BindingFlags.Instance);
MethodInfo generic=method.MakeGenericMethod(类型);
generic.Invoke(this,null);
}
}
私有无效初始值()
{
//2.初始化T类的实体
}
}
派生的公共类:基
{
public-Derived():基(typeof(Entity))
{
//3.派生构造函数代码
}
}

但是,在初始化实体之后执行派生构造函数代码当然有一个限制。

如果您不必直接调用
InitializeEntity
,但只想调用它,请将某种标志传递给基构造函数。要解决
T
问题,可以使用反射

public class Base
{
    protected Base(Type type = null)
    {
        // 1. Base constructor code
        if (type != null)
        {
            // this will to InitializeEntity<type>();
            MethodInfo method = typeof(Base).GetMethod("InitializeEntity", BindingFlags.NonPublic | BindingFlags.Instance);
            MethodInfo generic = method.MakeGenericMethod(type);
            generic.Invoke(this, null);
        }
    }
    private void InitializeEntity<T>()
    {
        // 2. Initialize entities of class T
    }
}

public class Derived : Base
{
    public Derived() : base(typeof(Entity))
    {
        // 3. Derived constructor code
    }
}
公共类基
{
受保护的基(类型=null)
{
//1.基本构造函数代码
if(type!=null)
{
//这将导致初始化属性();
MethodInfo method=typeof(Base).GetMethod(“InitializeEntity”,BindingFlags.NonPublic | BindingFlags.Instance);
MethodInfo generic=method.MakeGenericMethod(类型);
generic.Invoke(this,null);
}
}
私有无效初始值()
{
//2.初始化T类的实体
}
}
派生的公共类:基
{
public-Derived():基(typeof(Entity))
{
//3.派生构造函数代码
}
}

但是,当然存在一个限制,即派生构造函数代码将在初始化实体后执行。

为什么不使其受保护?@SimonWhitehead请查看更新不可能。您可以