Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
| Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old March 7th, 2005, 08:01 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Copenhagen, , Denmark.
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dynamic Cast in C#

I am trying to figure out how to do a more general method for doing some attribute retrieval, however my problem is that I do not know how to do the dynamic cast in C#, and it must be possible.

Previously I did something like this...
Code:
attribute = (MyType)Attribute.GetCustomAttribute(t, typeof(MyType));
... which worked perfectly, however I want to put this into a more general method, where MyType is given as argument (Type) to the method, so I would get something like this...
Code:
attribute = Attribute.GetCustomAttribute(t, attributeType);
... but it still needs the casting of the result from GetCustomAttribute, so how do I do this? I tried to use the as keyword but with no luck.

Thanks, Jacob.
__________________
Danish audio books for download at http://www.lytenbog.dk (Danske lydbøger til download).
  #2 (permalink)  
Old March 7th, 2005, 08:12 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Copenhagen, , Denmark.
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ups... I have just realized that I can interface myself out of this problem...
Code:
attribute = (IMyAttribute)Attribute.GetCustomAttribute(t, attributeType);
Still if anyone thinks this is an ugly solution let me know. Naturally I would like to do it the right way.

Thanks anyway, Jacob.
  #3 (permalink)  
Old March 23rd, 2005, 12:04 PM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:I do not know how to do the dynamic cast in C#, and it must be possible
as far as I know I don't think Dynamic cast would be possible in C#,

cast is an early binding task,you should know the types you want to cast at compile time.

_____________
Mehdi.
software student.
  #4 (permalink)  
Old March 23rd, 2005, 01:28 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Copenhagen, , Denmark.
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

No, not nessesarily :)

If you have several classes which are derived from the same parent class, you might not know what type of class you are dealing with. Say you made a method which as input would take some kind of animal class and this class was used to make child classes such as Dog, Cat etc., then it would be nice to dynamically cast the objects that was given to the method (Dog, Cat etc).

In C++ this is done using dynamic_cast, and if it returns null it is not of the type you are trying to cast it to. An example from C++ in this case would be...
Code:
BlockA* ba = dynamic_cast<BlockA*>(eblock);
if(ba != NULL)
{
...
}
The problem I had was a bit different since I wanted to give the type, into which I would cast, as an argument to some method.

Jacob.
  #5 (permalink)  
Old March 23rd, 2005, 04:40 PM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

yes,exactly
although my meaning from dynamic cast was different from C++
I meant we can't do any casting at runtime
Quote:
quote:The problem I had was a bit different since I wanted to give the type, into which I would cast, as an argument to some method.
you wanted to give a System.Type to your method as a argument(at runtime)and then did a casting to that Type..did you do this?

_____________
Mehdi.
software student.
  #6 (permalink)  
Old March 23rd, 2005, 11:52 PM
Registered User
 
Join Date: Mar 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am interested in this dynamic casting discussion. I have been trying to do it in C# but can't figure out how. Instead of casting using something like

(Int32) y

In my app, I won't know that y is Int32 at coding time. That info will be loaded from a file or a database. So, if the string variable t is set to "Int32" to hold the type of y, I want to be able to do something like

(Type.GetType("System."+t)) y

to cast y to Int32.

I'm guessing you can't do this because it doesn't seem to work when I try (I've tried other variations also), but it seems like there has to be a way. Otherwise how can database programs cast since you often don't know the type of variables until the program runs. I'm a longtime programmer, but fairly new to C# so maybe there is an easy way out that I just don't know.

Thanks for any help.

  #7 (permalink)  
Old March 24th, 2005, 07:57 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Copenhagen, , Denmark.
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Mehdi and Paulie...

Quote:
quote:you wanted to give a System.Type to your method as a argument(at runtime)and then did a casting to that Type..did you do this?
No, I didn't do that, but I used an interface for the group of possible classes to give as an argument, so in my example all possible types given to the method should implement the interface IMyAttribute.

Quote:
quote:Instead of casting using something like

(Int32) y

In my app, I won't know that y is Int32 at coding time. That info will be loaded from a file or a database. So, if the string variable t is set to "Int32" to hold the type of y, I want to be able to do something like

(Type.GetType("System."+t)) y
That is also what I would like to have accomplished, but I have not been able to do that yet. Still interested in the solution, though. It must be possible due to the quite flexible nature of C#/.NET.

Jacob.
  #8 (permalink)  
Old March 24th, 2005, 09:42 AM
Registered User
 
Join Date: Mar 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You said you "interfaced" yourself out of the problem. Are you able to do what I was trying to do above with that method? I need to learn more about interfaces I guess, but it does seem like there should be an easier way.

Paul


  #9 (permalink)  
Old March 24th, 2005, 01:44 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Copenhagen, , Denmark.
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Paul...

Quote:
quote:You said you "interfaced" yourself out of the problem. Are you able to do what I was trying to do above with that method? I need to learn more about interfaces I guess, but it does seem like there should be an easier way.
No, I am not yet able to do what you would like... and I would like. To get smarter I would still like to know the answer when it finally appears :)

What I did to solve my problem was to use an interface for all the possible classes it could be. Look at the following code which is an extract from a method I have...
Code:
// Getting an array of file information about the files matching the
// pattern given.
info = new DirectoryInfo(folder).GetFiles(filePattern);
foreach(FileInfo f in info)
{
    a = Assembly.LoadFrom(f.FullName);
    foreach(Type t in a.GetTypes())
    {
        // Retrieving the attribute with type attributeType if 
        // existing...
        attribute = (IPluginAttribute)Attribute.GetCustomAttribute(t
            , attributeType);
        // If the attribute is found and the property Name equals
        // what we are looking for the assembly is returned.
        if(attribute != null && attribute.Name.Equals(name))
        {
            this.type = t;
            this.assembly = a;
        }
    }
}
The above code is part of a custom-made plugin-handler, which loads assemblies and check if some specific attribute is set for some class. If it is I have the right class, which is the plugin module.

However in order to make this general (since it require a type cast) I had to make the different attributes implement an interface, which I then can use for the cast. In this case the interface is named IPluginAttribute and as it can be seen it has the property Name.

Hope it helps, Jacob.
  #10 (permalink)  
Old March 24th, 2005, 01:49 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Copenhagen, , Denmark.
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I might add that you can see from above code what I was trying to do initially... i.e to give the type as an argument and then cast using that type.

Jacob.




Similar Threads
Thread Thread Starter Forum Replies Last Post
invalisd cast msinha199 ASP.NET 2.0 Professional 3 May 28th, 2008 07:08 AM
Specified cast is not valid heba ASP.NET 2.0 Professional 1 May 27th, 2007 08:51 PM
"Specified cast is not valid" help BaBaBooey ASP.NET 1.0 and 1.1 Basics 2 November 23rd, 2004 12:12 PM
Specified cast is not valid TheMole BOOK: ASP.NET Website Programming Problem-Design-Solution 9 November 1st, 2004 05:09 PM
cast problem vickyj General .NET 6 September 8th, 2004 06:21 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.