Using reflection to cast an object - how?

G

Guest

I have an application where...

1) I know the class name of the object I want to instantiate.
2) The class uses the class factory approach to create objects.
3) The factory method is a static member of the class. The name of this
method
is always "New" plus the class name.

So I can use reflection to create my object (in C#)...

// create the fully qualified name of the class
// Note dfla.AttributeType.BpaType is a string
string qualTypeName = "DescriptorLib." + dfla.AttributeType.BpaType;
// get the Type
Type valType = Type.GetType(qualTypeName);
// create the static method name to call... "New" plus the class name
string methodName = "New" + dfla.AttributeType.BpaType;
// invoke the static method - it takes no parms and returns a "valType" object
object val = valType.InvokeMember
(methodName,BindingFlags.InvokeMethod,null,null, new
object[] {});

All this works, but what I want is a "valType" object. How do I cast object
"val" to "valType"?

// d.att is a "valType" object attribute
d.att = val as valType; // Doesn't work. Compiler doesn't recognize valType
d.att = (valType)val; // Doesn't work. Compiler doesn't recognize valType

Any ideas anyone?

Thanks.

BBM
 
J

Jon Skeet [C# MVP]

BBM said:
I have an application where...

1) I know the class name of the object I want to instantiate.
2) The class uses the class factory approach to create objects.
3) The factory method is a static member of the class. The name of this
method
is always "New" plus the class name.

So I can use reflection to create my object (in C#)...

// create the fully qualified name of the class
// Note dfla.AttributeType.BpaType is a string
string qualTypeName = "DescriptorLib." + dfla.AttributeType.BpaType;
// get the Type
Type valType = Type.GetType(qualTypeName);
// create the static method name to call... "New" plus the class name
string methodName = "New" + dfla.AttributeType.BpaType;
// invoke the static method - it takes no parms and returns a "valType" object
object val = valType.InvokeMember
(methodName,BindingFlags.InvokeMethod,null,null, new
object[] {});

All this works, but what I want is a "valType" object. How do I cast object
"val" to "valType"?

// d.att is a "valType" object attribute
d.att = val as valType; // Doesn't work. Compiler doesn't recognize valType
d.att = (valType)val; // Doesn't work. Compiler doesn't recognize valType

Any ideas anyone?

What good would casting do if you don't know the type ahead of time?
Generally, the thing to do is cast to an interface that you know the
type implements.
 
M

Marcos Stefanakopolus

why not just this?

(typeof(valType)) val = valType.InvokeMember
(methodName,BindingFlags.InvokeMethod,null,null, new
object[] {});
 
J

Jon Skeet [C# MVP]

Marcos Stefanakopolus said:
why not just this?

(typeof(valType)) val = valType.InvokeMember
(methodName,BindingFlags.InvokeMethod,null,null, new
object[] {});

Because that won't compile either - (typeof(valType)) won't compile as
an expression because valType isn't the name of a type, and even if it
did, the whole declaration wouldn't compile because "(typeof
(valType))" isn't the name of a type, which is what is needed for a
variable declaration.
 
R

Romain TAILLANDIER

I have a work around.
If you always know that method "New" exists, all the object you create
verify an interface or abstract class IMyClass
That way you can all cast into a common IMyClass. You can also add all
common properties or method in that interface or abstract class

Hope that's help

interface IMyClass
{
IMyClass New(); // instead of New + ClassName
}

public Class MyClass1 : IMyCLass
{
public IMyClass New();
}

public Class MyClass2 : IMyCLass
{
public IMyClass New();
}
 
G

Guest

Thanks to all responders. Marcos, the InvokeMember method returns an
"object" so compilation fails if you try to assign it to another type without
casting.

I used the common interface solution. Thanks Jon and Romain.
 

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

Top