Creating object but not using the standart way :(

  • Thread starter Thread starter CreateObject
  • Start date Start date
C

CreateObject

Assume that I have the classes below;

class mercedes: IAuto {
....
}

class ford: IAuto{
....
}

class audi: IAuto{
....
}

Is it possible to create objects derived from these classes by just using
strings. In other words, CreateObject("mercedes") will return mercedes
object and so CreateObject("ford") will give me the ford one...

It is important for me that the way you are suggesting must not use the
conditional things like if or switch.
How can I do that?

Thanks.
 
Look into the Reflection namespace. Reflection allows you to create
objects and invoke their properties, methods, etc. where their types
are not known at compile time.
 
HI,


Of course :)

Take a look at CreateInstance method, two classes implement it, AppDomain &
Activator , depending of what you have ( assembly where they were defined,
complete name, etc) you can use either one.
 
Thanx for your rapid answers :)
However, I got the following error :(

System.TypeLoadException: Could not load type 'NameSpaceThing.Class1' from
assembly 'App_Web_xcjnboz6, Version=0.0.0.0, Culture=neutral,
PublicKeyToken=null'.


Class1 myCla1 = new Class1();
//works fine
object px = Activator.CreateInstance(null, "NameSpaceThing.Class1");
//throws error

namespace NameSpaceThing
{
public class Class1
{
public Class1()
{

}
}
}
 
Hi,

Is the code in the same assembly where you define your class?
from the error message it seems not.

what if you call it like:
object px = Activator.CreateInstance( "NameSpaceThing" , "Class1");
 
My class is not in the same assembly where I call Activator.CreateInstance()
method.

these are differen classed stored in different files under different paths
Default.cs
namespace NameSpaceThing
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Class1 objCls = new Class1();
object px = Activator.CreateInstance(null,
"NameSpaceThing.Class1");
}
}
}

and my class Class1.cs

namespace NameSpaceThing
{
public class Class1
{
public Class1()
{
...
}
}
}

I also tried what you've suggested but nothing has changed
again :(
 
CreateObject said:
Assume that I have the classes below;

class mercedes: IAuto {
...
}

class ford: IAuto{
...
}

class audi: IAuto{
...
}

Is it possible to create objects derived from these classes by just using
strings. In other words, CreateObject("mercedes") will return mercedes
object and so CreateObject("ford") will give me the ford one...

It is important for me that the way you are suggesting must not use the
conditional things like if or switch.
How can I do that?

Thanks.

A lot of people are going to tell you how to do this with reflection but,
except when using plugins, reflection is not necessary and should be avoided
wherever possible for security, maintainability and correctness reasons.

You can avoid switch statements by using a dictionary to look up a delegate
to construct the appropriate object.

Of course the dictionary has to be built explicitly but at least you know
that you are not going to get a security exception and you know and can
control exactly what can and cannot be instantiated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top