Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C#

C# C# Discussion

Reply
 
Thread Tools Display Modes
Old 07-09-2007, 03:20 AM   #1 (permalink)
Forum Novice
 
Join Date: Jan 2005
Age: 25
Posts: 125
Send a message via MSN to Kaon
Default Some question of C#

First Question : typeof(classX) -> typeX ... Y(typeX) -> classX
Here is my problem : I know how to get the type of a class or an instance. For this, using typeof(MyClass) or MyInstance.GetType() works great.

But, know, I am facing a problem : I want to call a templated function with the type of instance ... MyFunction<ClassOfMyInstance>(MyInstance)

Is there a keyword that do the opposite of typeof ?

---------------------

Second question :
I have a generic List (List<Something>). How to get the type Something with the instance of my List ?

----------------------
Thanks
__________________
Kaon is offline   Reply With Quote
Old 07-12-2007, 05:25 AM   #2 (permalink)
Forum Novice
 
Join Date: Jan 2005
Age: 25
Posts: 125
Send a message via MSN to Kaon
Default

no answer?

Do I have misformulated my questions?
__________________
Kaon is offline   Reply With Quote
Old 07-12-2007, 11:52 PM   #3 (permalink)
ConnectUO Creator
 
Jeff's Avatar
 
Join Date: Jan 2004
Age: 27
Posts: 4,822
Default

Do you mean, you want to create a Instance of a type? If so, you can use

Activator.CreateInstance(typeof(MyClass));

I believe there is a Generics method to this also.

Activator.CreateInstance<MyClass>();

More info here Activator.CreateInstance Method (System)
__________________
Jeff Boulanger
ConnectUO - Core Developer

Want to help make ConnectUO better? Click here to submit your ideas/requests
Use your talent to compete against other community members in RunUO hosted coding competitions

If you know XNA (even if its just a little) or are a good artist(2d or 3d) and are interested in making games for a hobby send me a pm or drop by #xna in irc.runuo.com. I'm looking to put together a small game development team.


Please do not pm me for support. If you are having issues please post in the appropriate forum. Thanks for your continued support of both ConnectUO and RunUO
Jeff is offline   Reply With Quote
Old 07-13-2007, 03:08 AM   #4 (permalink)
Forum Novice
 
Join Date: Jan 2005
Age: 25
Posts: 125
Send a message via MSN to Kaon
Default

For the first question, here's what I want to do :
I have an instance of a List<Something> called for example toto. I can get the Type object of "Something" by get through the toto.GetType().GenericParameters (or a property approching this, it is the answer to my second question of my first post )

Now, I want to call a method MyMethod<T>(object blah), with the parameters I found : MyMethod<Something>(toto)

But, I don't want to do a succession of "if" condition to catch all type that can be "Something" ...

I know that typeof(Something) return the Type object of "Something"
Now, I just want a way to reverse this : how to get the "Something" from the Type object of "Something" ?

Or, in my case, how to call my generic method with a type I don't know ?
__________________
Kaon is offline   Reply With Quote
Old 07-13-2007, 07:11 AM   #5 (permalink)
RunUO Forum Moderator
 
daat99's Avatar
 
Join Date: Dec 2004
Location: Israel
Age: 27
Posts: 8,163
Send a message via ICQ to daat99 Send a message via AIM to daat99
Default

Quote:
Originally Posted by Kaon View Post
For the first question, here's what I want to do :
I have an instance of a List<Something> called for example toto. I can get the Type object of "Something" by get through the toto.GetType().GenericParameters (or a property approching this, it is the answer to my second question of my first post )

Now, I want to call a method MyMethod<T>(object blah), with the parameters I found : MyMethod<Something>(toto)

But, I don't want to do a succession of "if" condition to catch all type that can be "Something" ...

I know that typeof(Something) return the Type object of "Something"
Now, I just want a way to reverse this : how to get the "Something" from the Type object of "Something" ?

Or, in my case, how to call my generic method with a type I don't know ?
Do you have an actual object in the list or is it just a list of object types?
If so, do you want to execute the method for that particular object?

Assuming both answers are yes you can do something like this:
Code:
try
{
	//get the method that we want based on its name (executeName) that expect 1 argument of type "CommandParameters" (an object)
	//ecType is the type of the object you want to execute the method for
	MethodInfo method = ecType.GetMethod(executeName, new Type[] { typeof(CommandParameters) }); 
	//execute the method by sending it the object it expects and return the value it returns to the caller
	return (bool)method.Invoke(null, new object[] { command });
}
catch (Exception ex)
{
	//it failed, throw the exception (crash the shard) up the line
	//if you know how to handle the exception then do so here instead of throwing the exception so it won't crash the shard
	throw ex;
}

Hope that helps
__________________
I always try to help
Sometimes, I don't know how....

My Web Page
Forum Rules
-------------------------------------------------------------
Extensive OWLTR System | Token System | World Teleporters
-------------------------------------------------------------
daat99 is offline   Reply With Quote
Old 07-13-2007, 11:35 AM   #6 (permalink)
ConnectUO Creator
 
Jeff's Avatar
 
Join Date: Jan 2004
Age: 27
Posts: 4,822
Default

I really don't understand, perhaps you should show us some code, it still sounds to me that you are refering to the oposite of typeof(object) would be an instance of that object. I would need to see code as to what you are doing to help you further.
__________________
Jeff Boulanger
ConnectUO - Core Developer

Want to help make ConnectUO better? Click here to submit your ideas/requests
Use your talent to compete against other community members in RunUO hosted coding competitions

If you know XNA (even if its just a little) or are a good artist(2d or 3d) and are interested in making games for a hobby send me a pm or drop by #xna in irc.runuo.com. I'm looking to put together a small game development team.


Please do not pm me for support. If you are having issues please post in the appropriate forum. Thanks for your continued support of both ConnectUO and RunUO
Jeff is offline   Reply With Quote
Old 07-13-2007, 03:13 PM   #7 (permalink)
Forum Expert
 
Join Date: Nov 2002
Location: Shawinigan, Québec, Canada
Age: 25
Posts: 344
Default

Check that:
System.Type.GetGenericArguments
and
System.Reflection.MethodInfo.MakeGenericMethod
ZixThree is offline   Reply With Quote
Old 07-13-2007, 04:38 PM   #8 (permalink)
Forum Novice
 
Join Date: Jan 2005
Age: 25
Posts: 125
Send a message via MSN to Kaon
Default

Interesting!

Thanks ZixThree, you put me on the path to the solution I guess
I'll try to use System.Reflection.MethodInfo.MakeGenericMethod and post if it works (or not ^^) in a few days

Thanks daat99 and Jeff to, Reflection is not yet my "cup of tee", so it's always good to read documentation about it
__________________
Kaon is offline   Reply With Quote
Old 07-13-2007, 06:11 PM   #9 (permalink)
ConnectUO Creator
 
Jeff's Avatar
 
Join Date: Jan 2004
Age: 27
Posts: 4,822
Default

Quote:
Originally Posted by Kaon View Post
Interesting!

Thanks ZixThree, you put me on the path to the solution I guess
I'll try to use System.Reflection.MethodInfo.MakeGenericMethod and post if it works (or not ^^) in a few days

Thanks daat99 and Jeff to, Reflection is not yet my "cup of tee", so it's always good to read documentation about it
Something to consider, Reflection is slow and resource hungry, avoid it whenever possible.
__________________
Jeff Boulanger
ConnectUO - Core Developer

Want to help make ConnectUO better? Click here to submit your ideas/requests
Use your talent to compete against other community members in RunUO hosted coding competitions

If you know XNA (even if its just a little) or are a good artist(2d or 3d) and are interested in making games for a hobby send me a pm or drop by #xna in irc.runuo.com. I'm looking to put together a small game development team.


Please do not pm me for support. If you are having issues please post in the appropriate forum. Thanks for your continued support of both ConnectUO and RunUO
Jeff is offline   Reply With Quote
Old 08-07-2007, 03:25 PM   #10 (permalink)
Forum Novice
 
Join Date: Jan 2005
Age: 25
Posts: 125
Send a message via MSN to Kaon
Default

Ok, I've tested some things, and now I'm faced to a error I can't explain : "wrong parameters count"

Code:
Exception:
System.Reflection.TargetParameterCountException: Nombre de paramètres incorrects.
   à System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   à System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   à System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
   à System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
   à Server.Gumps.ASetListGump`1..ctor(PropertyInfo prop, Mobile mobile, Object o, Stack stack, Int32 page, ArrayList list)
   à Server.Gumps.ASetListGump`1.InternalTarget.OnTargetFinish(Mobile from)
   à Server.Targeting.Target.Invoke(Mobile from, Object targeted)
   à Server.Network.PacketHandlers.TargetResponse(NetState state, PacketReader pvSrc)
   à Server.Network.MessagePump.HandleReceive(NetState ns)
   à Server.Network.MessagePump.Slice()
   à Server.Core.Main(String[] args)
Here is the code :
Code:
public ASetListGump(PropertyInfo prop, Mobile mobile, object o, Stack stack, int page, ArrayList list) : base(GumpOffsetX, GumpOffsetY)
{
[...]
            object lst = prop.GetValue(o,null);
            int count = (int)lst.GetType().GetProperty("Count").GetValue(lst, null);
[...]
            for (int i = 0, k=1; i < count; ++i)
            {
[...]
                object test = prop.GetValue(o, new object[] { i });
[...]
            }
[...]
}
It crashes on this line :
object test = prop.GetValue(o, new object[] { i });

o is an object with a property of type List<Item>. When the list is empty, it works (normal, it doesn't enter the for iteration) ... there is a "i" index, because of Count property.

I've "googled" a lot, but all example I found is like I wrote
Any ideas?
__________________
Kaon is offline   Reply With Quote
Old 08-08-2007, 08:27 PM   #11 (permalink)
Forum Expert
 
Join Date: Aug 2004
Location: Redmond, WA
Age: 21
Posts: 1,288
Send a message via AIM to Sep102 Send a message via MSN to Sep102
Default

Umm, it looks like you might be getting a little mixed up there and that would be the problem.

The PropertyInfo parameter "prop" (I would assume from your explanation) is used to access some property of the Object "o" that returns an object of type List<Item>, which is not an indexer (as in, it isn't a property that takes any arguments), so passing null for the arguments in the first prop.GetValue() line works fine and gets the list like it's supposed to.

However, then you attempt to use the PropertyInfo again on "o" to get at the elements of the list, which isn't working (and thus causing an exception) because it's attempting to pass an argument to a property that does not have any parameters.

Now, what you might have meant to do is make the second call to GetValue() with "lst" as the object rather than "o", but that still wouldn't work and would also throw an exception (of System.Reflection.TargetException, assuming o's type is not a subtype of List<Item>).

What I assume you were trying to do was to access the default property of the list object (i.e. the one that allows you to put list_name[0] to retrieve the first element), which means (I'm assuming you want to keep it generic, so I'm not going to suggest casting) that you'll need to create a new PropertyInfo that gets the property "Item" of the lst object's type (assuming the type uses the default name for the default property, which List does), then use that PropertyInfo with the call to GetValue() and pass "lst" instead of "o".
Sep102 is offline   Reply With Quote
Old 08-09-2007, 04:16 AM   #12 (permalink)
Forum Novice
 
Join Date: Jan 2005
Age: 25
Posts: 125
Send a message via MSN to Kaon
Default

Quote:
the Object "o" that returns an object of type List<Item>, which is not an indexer (as in, it isn't a property that takes any arguments)
A property (get/set) never takes arguments, unless it is a array type and use [i] to access index ... no? How do you declare a indexer ? What it is? :/ (just to know, disgression of the actual problem ^^)


Quote:
What I assume you were trying to do was to access the default property of the list object (i.e. the one that allows you to put list_name[0] to retrieve the first element), which means (I'm assuming you want to keep it generic, so I'm not going to suggest casting) that you'll need to create a new PropertyInfo that gets the property "Item" of the lst object's type (assuming the type uses the default name for the default property, which List does), then use that PropertyInfo with the call to GetValue() and pass "lst" instead of "o".
(exactly what I want ^^)
I searched such a property with name "this" ( public T this[int index] { get; set; }) or others in the metadata definition of List<> ... And I checked again the metadata, and can't find the Item Property ... where is it defined ? I checked the herited classes too :/

Without testing, the first thing coming up to my mind is that it won't find any property with the name "Item" :/
Can you explain a litlle bit more ?

Waiting to know more on this point, I used to call the GetEnumerator() methods, and cast it to a IEnumerator ... It works for the moment, but I guess I'll be limited on the futur with this ...
__________________
Kaon is offline   Reply With Quote
Old 08-10-2007, 10:42 PM   #13 (permalink)
Forum Expert
 
Join Date: Aug 2004
Location: Redmond, WA
Age: 21
Posts: 1,288
Send a message via AIM to Sep102 Send a message via MSN to Sep102
Default

Sorry, guess I should have explained a few terms better.

Quote:
Originally Posted by Kaon
A property (get/set) never takes arguments, unless it is a array type and use [i] to access index ... no? How do you declare a indexer ? What it is? :/ (just to know, disgression of the actual problem ^^)
An indexer is a property that "is a array type and use [i] to access index", it's one of the many names for properties that are accessed as "[i]" (which, as I stated, is a property that takes arguments, likewise you could have a property that takes two arguments and access it as "[i1, i2]"). And, as you probably know already (knowing what an indexer is now) you declare an indexer like "public Int32 this[Int32 index] ..."

Quote:
Originally Posted by Kaon
(exactly what I want ^^)
I searched such a property with name "this" ( public T this[int index] { get; set; }) or others in the metadata definition of List<> ... And I checked again the metadata, and can't find the Item Property ... where is it defined ? I checked the herited classes too :/

Without testing, the first thing coming up to my mind is that it won't find any property with the name "Item" :/
Can you explain a litlle bit more ?
If you used Visual Studio's Object Browser to look for it, then you're correct, you won't find an Item property in List (however you will if you use Reflector to browse classes, which, by the way, is quite an invaluable tool). As I said, it does have an Item property, as that is the default name that it gives to a class's array properties. So, while you won't be able to find it in the Object Browser, it does exist, and is the way you access the array property when using Type.GetProperty() (so, to access List's this[Int32] property, you would use prop2 = lst.GetType().GetProperty("Item"), then you could access it the way that you were trying to).

Last edited by Sep102; 08-10-2007 at 10:47 PM.
Sep102 is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5