|
 |
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 .
|
 |
|
|
 |
|

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
|
|
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.
|

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
|
|
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.
|

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
|
|
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.
|

January 3rd, 2006, 12:45 PM
|
Registered User
|
|
Join Date: Jan 2006
Location: , Pennsylvania, .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|

April 5th, 2006, 09:25 AM
|
Registered User
|
|
Join Date: Apr 2006
Location: , , France.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

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
|
|
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
|
Thread Tools |
Search this Thread |
|
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |