First of all I'd like to say that I am not a "code guru", I do programming but may not be the best guy to listen to advice :)
This last year I've been dabbling with C# working on a network app-framework project with some colleagues of mine. It has been a great experience and I definitely will continue to explore C# as time goes by.
I'll give here homage to two features of the .Net platform, the foreach construct and Reflection as used in C#.

I'll quote the C# Programming Reference:

"The foreach statement repeats a group of embedded statements for each element in an array or an object collection. The foreach statement is used to iterate through the collection to get the desired information, but should not be used to change the contents of the collection to avoid unpredictable side effects."

It's usage is simple:
foreach (Type variable_name in expression){...do stuff }


where expression can be either an array, or a SomeClass.GetItems() that returns an array or SomeClass.Items Collection.

For the most things I did up until now the main entry point in doing reflection would go over the Type Class.
So to make an example if I'd like to look into the inner guts of a class (ex. TestClass) this might be a nice way of doing it.

TestClass tc= new TestClass();
Type _type=tc.getType;

now the System.Type Class is a MASSIVE thing, providing a huge number of Methods and Properties so I'll be demonstrating only some basic stuff, like getting all the members. Easier said then done they can be get by doing _type.GetMembers(), so why not do:
foreach(MemberInfo mi in type.GetMembers()){

}

Now this extracts a bunch of stuff and you might not need them all, one nice thing is the MemberTypes Enum so you can check if the Members are for example Methods, Constructors, Properties ecc.. I would have liked to point something to the MSDN Documentation but I don't have the URLs, but simply looking for the Class or Enum Type on the http://msdn.microsoft.com suffices for most of the occasions.

Now System.Reflection.MemberInfo is a cute thing and you can get a lot of info out of it but not everything, the nice thing is, you can cast it to more specialized types as:
System.Reflection.EventInfo
System.Reflection.FieldInfo
System.Reflection.MethodBase
System.Reflection.PropertyInfo
>System.Type

So it's possible to do
PropertyInfo pi=(PropertyInfo)mi;

and that leads to extensive info on properties ecc, the same principle goes for the other types listed.


When I was doing stuff on it at first glimpse I failed to see System.Type and I had a lot of troubles in understanding if the mi was a Delegate, but once done
Type mytype=(Type)_mi;

I was able to do something like
if(mytype.IsSubclassOf(Type.GetType("System.Delegate")))

See you soon! :)

Comments

Popular posts from this blog

Relaxing SSL validation for JaxWS

Kaspersky Anti Virus 6 and Subversion problems

Search and Replace in ODT using AODL