Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #11 (permalink)  
Old March 24th, 2005, 05:39 PM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

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:
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:
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 With Quote
  #12 (permalink)  
Old March 25th, 2005, 05:13 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Copenhagen, , Denmark.
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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:
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.
Code:
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 With Quote
  #13 (permalink)  
Old March 25th, 2005, 02:13 PM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

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:
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 With Quote
  #14 (permalink)  
Old January 3rd, 2006, 12:45 PM
Registered User
 
Join Date: Jan 2006
Location: , Pennsylvania, .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

http://www.codeproject.com/csharp/Fa...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 With Quote
  #15 (permalink)  
Old April 5th, 2006, 09:25 AM
Registered User
 
Join Date: Apr 2006
Location: , , France.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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 With Quote
  #16 (permalink)  
Old September 25th, 2006, 11:37 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Harrisburg, PA, USA.
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

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/lib...einstance.aspx

Brian
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 07:42 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.