"ssamuel" <ss*****@gmail.comwrote in message
news:11**********************@f16g2000cwb.googlegr oups.com...
You know the type info in a static method, so there's no reason to get
it from anywhere. Static methods are contained and runnable only in a
given type, so the current type, and therefore the type info, is known
at design time and fixed.
Well, I guess thats a limitation. I can think of many reasons why I would
want to know the type in a static method and won't know the type at compile
time. For instance suppose I wanted a class that contains data for a
listview that could query itself using reflection and find all its public
properties and then use that information to generate the column headers of a
listview. At the initialization stage when its setting up the UI, there
won't be an instance of the class since it hasn't hit the database yet, but
the class inherently contains all the information needed to set up the
coloumn headers and can do so via a static method. But the problem is that
any classes based on this class doesn't know what its type is unless its
passed in
public abstract class ListViewData{
public ListViewData() {
}
public abstract object this[string index] {
get;
set;
}
/// <summary>
/// Sets up the column headers for the ListView
/// </summary>
public static void SetListViewColumnHeaders(Type type,
ListView.ColumnHeaderCollection headers) {
PropertyInfo[] infoList = type.GetProperties(BindingFlags.Public |
BindingFlags.Instance);
foreach (PropertyInfo p in infoList) {
foreach (Attribute attribute in Attribute.GetCustomAttributes(p)) {
GridViewColumnAttribute a = (GridViewColumnAttribute) attribute;
if (a == null || a.Include == false)
continue;
headers.Add(a.Name, a.Width).Tag = new GridViewColumnTag(p.Name,
p.PropertyType, a.Flags);
}
}
}
}
In your example, you seem to want to know if you're calling Method() on
C or D. The fact is that you're always and only calling it on C,
because there is no D.Method() per se. D appears to have a static
Method() because it inherited it from its base, C, but you're still
calling it on C, not D.