2

Browsing the class System.Dynamic.DynamicObject, I find it is concrete (not-abstract) class, but preventing from creating an instance of it directly by making its default constructor protected.

So what is the point of not making it just abstract with a public constructor ?

Clarification:

class Base1
{
    protected Base1() // protected constructor, concrete class
    {
    }
}
class Derived1 : Base1
{
    public Derived1() : base()
    {
    }
}

abstract class Base2
{
    public Base2() // public constructor, abstract class
    {
    }
}
class Derived2 : Base2
{
    public Derived2() : base()
    {
    }
}
4
  • that's a good question mate. I hope someone will answer that soon, because I'm interested to know that myself too ! At first, I was going to tell you that maybe it was because only children should have access to concrete objects but abstract classes should do the trick Commented Jun 10, 2015 at 11:39
  • Well, it doesn't have any abstract methods. You only need to use the abstract keyword to enable abstract methods, so perhaps they simply only use abstract on abstract classes with abstract methods? Commented Jun 10, 2015 at 11:39
  • C# fully permits you to declare abstract class without any abstract member Commented Jun 10, 2015 at 11:51
  • keyword abstract is not used to restrict access to constructor, I guess it's the main point. class is not abstract but author decided to restrict access Commented Jun 10, 2015 at 12:42

2 Answers 2

2

With a protected constructor this is possible:

public class MyClass
{
    protected MyClass() { }

    public static MyClass GetInstance()
    {
        return new MyClass();
    }
}    

public class MyDerivedClass : MyClass
{
}

public static void Main()
{
    var myInstance = MyClass.GetInstance();
    var myDerivedInstance = new MyDerivedClass();

    var constructor = typeof(MyClass).GetConstructor(
                BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
                null,
                Type.EmptyTypes,
                null);

    var instanceFromReflection = (MyClass) constructor.Invoke();
}

An abstract MyClass can't be instantiated neither by MyClass.GetInstance() (compiler error at return new MyClass();), nor by the reflection kind (runtime error System.MemberAccessException: Cannot create an instance of MyClass because it is an abstract class.)

Sign up to request clarification or add additional context in comments.

2 Comments

Nothing new, it was discussed with @Desolator in comments.
Sure but discussions in the comments are no answers, especially not if the comments are collapsed...
1

There is a comment that explains

    /// <summary>
    /// Enables derived types to initialize a new instance of the <see cref="T:System.Dynamic.DynamicObject"/> type.
    /// </summary>
    [__DynamicallyInvokable]
    [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
    protected DynamicObject()
    {
    }

it's used somewhere to create objects of type System.Dynamic.DynamicObject

17 Comments

At first I thought "that's so stupid, obviously you can do that", but this may actually be the reason - if you need to pass a DynamicObject somewhere, but don't actually want any implementation (e.g. some kind of null-DynamicObject). Saves you the trouble of creating an unnecessary class NullDynamicObject : DynamicObject {}.
@Luaan I think your comment should be good if it is an answer!
@Daniel Your answer is just the question itself, not adding any valuable information.
@Desolator I'm not agree . There is in question about preventing to create directly, and nothing about creating from derived classes, if you make class abstract you won't be able to do it anymore.
I didn't understand? If I have an abstract class with public constructor (or even without declaring a constructor, just make it uses default constructor), I can do the same. (i.e. I cannot create directly too)
|

Your Answer

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