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 ?

Mohammad Dayyan
  • 21,578
  • 41
  • 164
  • 232

2 Answers2

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));
    }
}
Sherlock
  • 5,557
  • 6
  • 50
  • 78
  • 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 – Mohammad Dayyan Nov 29 '13 at 21:42
  • 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. – Sherlock Nov 29 '13 at 21:46
  • I have several `BusinessObject`s 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? – Mohammad Dayyan Nov 30 '13 at 03:38
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
    ...
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • Can we cast `yourObject` to `BaseBusinessObject`, If so how ? I have to convert `yourObject` to `BaseBusinessObject` for using its methods – Mohammad Dayyan Nov 29 '13 at 21:40
  • I have several `BusinessObject`s class in my project, I have to write if statement for each of them ? e.g: `ContentBusinessObject`, `BrandBusinessObject`, ... Is there a better way? – Mohammad Dayyan Nov 30 '13 at 03:36