Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / January 2005

Tip: Looking for answers? Try searching our database.

Using reflection to cast an object - how?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
BBM - 11 Jan 2005 18:47 GMT
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
Jon Skeet [C# MVP] - 11 Jan 2005 18:52 GMT
> I have an application where...
>
[quoted text clipped - 26 lines]
>
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Marcos Stefanakopolus - 11 Jan 2005 19:00 GMT
why not just this?

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

> I have an application where...
>
[quoted text clipped - 35 lines]
>
> BBM
Jon Skeet [C# MVP] - 11 Jan 2005 22:27 GMT
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Romain TAILLANDIER - 12 Jan 2005 10:17 GMT
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();
}

> I have an application where...
>
[quoted text clipped - 30 lines]
>
> BBM
BBM - 14 Jan 2005 02:05 GMT
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.  

> I have an application where...
>
[quoted text clipped - 30 lines]
>
> BBM

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.