1

I have the following classes :

public abstract class Repository<TEntity, TIdentifier> : IRepository<TEntity, TIdentifier> where TEntity : class
{ --- }

public abstract class BaseBusinessObject<TEntity, TIdentifier> : Repository<TEntity, TIdentifier> where TEntity : class
    { --- }

public class AttachmentBusinessObject : BaseBusinessObject<Attachment, long>
    { --- }

Somewhere, The only things that I have are "AttachmentBusinessObject" in string and "Attachment" as TEntity in string.

How can I create an instance of BaseBusinessObject or AttachmentBusinessObject ?

3
  • 2
    BaseBusinessObject is an abstract class, you cannot instantiate it. Commented Nov 29, 2013 at 21:28
  • @JeroenVannevel: So,How we can create instance from AttachmentBusinessObject ? Commented Nov 29, 2013 at 21:36
  • @Mohammad: using reflection? Trough the Activator. Look at the duplicate question for a lot more information. Commented Nov 29, 2013 at 21:37

2 Answers 2

2

You can do this using Activator.CreateInstance:

public class Base
{ }

public class Derived : Base
{ }

[TestMethod]
public void BaseDerivedTest()
{
    string type = "Derived"; // type is found somewhere upstream
    if (type == "Derived")
    {
        var b = Activator.CreateInstance(typeof(Derived).BaseType);
        Assert.IsInstanceOfType(b, typeof(Base));
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your response, but your solution doesn't work in my sample, because I have to cast b to BaseBusinessObject for using its methods
I don't see how it couldn't work for you. I made an edit for clarity. Please let me know if you need anything else.
I have several BusinessObjects class in my project instead of "Derived", I have to write if statement for each of them ? e.g: ContentBusinessObject, BrandBusinessObject, ... Is there a better way?
1

I'm not sure if I'm understanding your correctly, but you may want to check out Activator.CreateInstance.

Something like this:

var yourObject
    = Activator.CreateInstance(null, "AttachmentBusinessObject").Unwrap();

This isn't very useful though, if you want to pass the object around, because CreateInstance has no idea what the type might be, so it just returns an object.

One way around it - any classes you'll be creating an instance of can all implement a common interface. So you might end up with something like:

IBusinessObject yourObject
    = (IBusinessObject)Activator.CreateInstance(null, "AttachmentBusinessObject").Unwrap();

Another option is to use the first line of code above, and then test for the correct class type in an if/else statement:

Type objType = yourObject.GetType();

if (objType == typeof(AttachmentBusinessObject))
{
    var myAttachBusObject = (AttachmentBusinessObject)yourObject;
    ...
}
else
    ...

2 Comments

Can we cast yourObject to BaseBusinessObject, If so how ? I have to convert yourObject to BaseBusinessObject for using its methods
I have several BusinessObjects class in my project, I have to write if statement for each of them ? e.g: ContentBusinessObject, BrandBusinessObject, ... Is there a better way?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.