English 中文(简体)
是否可能与受保护的建筑商一起制造物体?
原标题:Is it possible to CreateInstance of object with protected constructor?

有关情况如下:

public abstract class BaseClass
{
    protected BaseClass()
    {
        // some logic here
    }

    protected BaseClass(Object parameter) : this()
    {
        // some more logic
    }
}

public class Descendant : BaseClass
{
   // no constructors
}

I m试图打电话Activator.CreateInstance,上载Decendant等舱,但no Constructionor

是否需要,以明确方式界定“在血统阶级上的定义?

使用的具有约束力的I ve是: 旗帜。 事例:无

<>说明1: 在现实中,我叫AppDomain.CreateInstanceAnd Unwrap(),如果它有某种影响的话。

 domain.CreateInstanceAndUnwrap(path, typeName, false, BindingFlags.Instance |
     BindingFlags.NonPublic, null, new[] { parameter }, null, null);

<<>说明2> 如果我明确地界定了Descendant类受保护的建筑商,那么该建筑商就会投入使用,但我希望尽可能避免这种情况。

最佳回答

< 你们不能使用行为者。 创造基础设施,但在完全信任的环境中,你应当能够直接通过思考找到并打电话给建筑商。

实际上,你的爱德森特班自动向受保护的基类建筑商提供穿过通道的公共建筑商。 这应当做到:

Activator.CreateInstance(typeof(Descendant))

奥凯现在我认识到,你再次试图援引非过失建筑商。 但不幸的是,这根本不允许。 由于基类是抽象的,在基类上挑出建筑商直接赢得了打工。 你们需要在后代阶层中有一名建筑商(由汽车制成或明确定义),以建造物体。

你可以在德斯森德班上设立一个穿透型建筑商,但你的评论表明,你试图避免迫使执行者在其建筑商中通过价值。 你可能真的想要的是,在构造目标之后,可以采用一种初步方法:

// Define other methods and classes here
public abstract class BaseClass
{
    protected BaseClass()
    {
        // some logic here
    }

    protected Object Parameter {get; private set;}    

    public virtual void Initialize(Object parameter)
    {
        Parameter = _parameter;
        // some more logic
    }
}
问题回答

似乎与你一样,是建筑模式,而不是建筑。 很简单,你界定了“购买”你想要的那类“建筑”。 与此类似(注:前面未经测试的代码):

public abstract class BaseBuildable : MarshalByRefObject {
   public String Foo { get; internal set; }
}

public class DerivedBuildable : BaseBuildable { }

public class BuildableBuilder : MarshalByRefObject {
   private String _foo;
   public BuildableBuilder WithFoo(String foo) { _foo = foo; return this; }
   public TBuildable Build<TBuildable>() where TBuildable : BaseBuildable, new() {
       return new TBuildable { Foo = _foo; }
   }
}

// Used so:
var builder = domain.CreateInstanceAndUnwrap(.. // yadda yadda, you want a BuildableBuilder
var buildable = builder.WithFoo("Foo, yo").Build();




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...