Subject: Dynamic Cast in C#
Posted By: jacob Post Date: 3/7/2005 7:01:43 AM
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...
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...
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.
Reply By: jacob Reply Date: 3/7/2005 7:12:08 AM
Ups... I have just realized that I can interface myself out of this problem...
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.
Reply By: mehdi62b Reply Date: 3/23/2005 11:04:13 AM
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.
Reply By: jacob Reply Date: 3/23/2005 12:28:47 PM
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...
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.

Reply By: mehdi62b Reply Date: 3/23/2005 3:40:13 PM
yes,exactly
although my meaning from dynamic cast was different from C++
I meant we can't do any casting at runtime
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.
Reply By: paulie100 Reply Date: 3/23/2005 10:52:57 PM
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.

Reply By: jacob Reply Date: 3/24/2005 6:57:06 AM
Hello Mehdi and Paulie...

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:
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.
Reply By: paulie100 Reply Date: 3/24/2005 8:42:24 AM
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


Reply By: jacob Reply Date: 3/24/2005 12:44:38 PM
Paul...

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...
// 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.
Reply By: jacob Reply Date: 3/24/2005 12:49:30 PM
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.
Reply By: mehdi62b Reply Date: 3/24/2005 4:39:24 PM
Jacob,
well,all of your attributes implement IPluginAttribute and then while you want to retrieve your attributes you cast them to IPluginAttribute..and IPluginAttribute should have a definition for Name property..ofcource IPluginAttribute is recognized at compile time
quote:
Originally posted by jacob

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.

I think you wanted a type cast for your several attributes so that you could be able to have the exact attribute and ...(instead of working on IPluginAttribute)

I think you needed to create the exact attribute not to create a base attribute (IPluginAttribute)
quote:
Originally posted by jacob

       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;
        }


did I guess your meaning correctly?

_____________
Mehdi.
software student.
Reply By: jacob Reply Date: 3/25/2005 4:13:21 AM
Mehdi...

What I am doing with the interface and what I wanted to do with the type given as argument is actually two different things, but seem to solve my problems.

quote:
I think you wanted a type cast for your several attributes so that you could be able to have the exact attribute and ...(instead of working on IPluginAttribute)
Therefore, the solution done with the interface is not the answer to my original question. I think it is a matter of opinion if the developer like the method argument approach or the interface approach. In kind of a psudo code I have tried to code what I wanted to do underneath.
public void LoadPluginAttribute(Type attributeType, string name)
{
    ...
    attribute = (attributeType)Attribute.GetCustomAttribute(t, attributeType);
    if(attribute != null && attribute.Name.Equals(name))
    {
        ...
    }
    ...
}
However in order to be able to retrieve the Name property I would have to make the attribute implement some interface anyways. Otherwise it would be a bad implementation.

What I did with the interface now seem to me like a good solution, since I would still have to make an interface for the classes due to the fact that the Name property has to be supported by every class which should be given as argument to the method, and therefore I might as well cast with the interface.

Jacob.
Reply By: mehdi62b Reply Date: 3/25/2005 1:13:01 PM
using the interface as you had done is a very good solution..

however you could  do some other things,but you know languages like C#
don't support such dynamic abilities.(dynamic cast)

I can't have a method could return different objects with different types,

I had a problem,I wanted to create some dynamic objects at runtime(with the given type) I did something else I created an object with the given type and with  ConstructorInfo, EventInfo, FieldInfo, MemberInfo, MethodInfo, ParameterInfo, and PropertyInfo...got the value I wanted..(for example in your case you could make the exact attribute you wanted and then get its desired property at runtime)..
quote:
Originally posted by paulie100

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


No,C# doesn't support such abilities,C# is a statically typed language and can't return different objects with different types at run time.

_____________
Mehdi.
software student.
Reply By: dave.dolan Reply Date: 1/3/2006 11:45:54 AM
http://www.codeproject.com/csharp/Fast_Dynamic_Properties.asp

It's the answer to all of your troubles, infact, it even has the same syntax you're looking for.

I about crapped when I saw it because I was just debating taking the time to learn this method of emitting so I can build an ORMapper for NON-relational data (yeah I know tough to find documentation on stuff like that out there because everyone uses RDBMS's, but I'm working with an experimental technology that isn't.. ) I'm quite happy with this little snippet.

Reply By: yohm31 Reply Date: 4/5/2006 9:25:52 AM
you can do easily dynamic casting and type manipulation with .Net 2.0 and the powerfull GENERICS

see more on http://www.15seconds.com/issue/031024.htm



Reply By: bmains Reply Date: 9/25/2006 11:37:08 AM
Hey,

I've been trying to achieve what you are trying to do.  I found most of the resources stating to use Activator.CreateInstance() method, available here:  http://msdn2.microsoft.com/en-us/library/system.activator.createinstance.aspx

Brian

Go to topic 50125

Return to index page 165
Return to index page 164
Return to index page 163
Return to index page 162
Return to index page 161
Return to index page 160
Return to index page 159
Return to index page 158
Return to index page 157
Return to index page 156