S
Sky
Hi: need a little help with Reflection on Static fields, from within Static
Methods:
If I define a class:
class Customers{
public int ID;
public string FirstName;
public string LastName;
}
and I want to get an array of these FieldNames WITHOUT INSTANTIATING THE
CLASS ... (to make SQL from, on the fly)... how do I do this?
Situation II, in case I go a little further...(and don't get lost!) what
about something like:
class XFieldInfo {
public string FieldName ="";
public System.Type FieldType;
public int FieldLength;
}
class Schema_Customer{
public static ID = new XFieldInfo("ID",System.Int32, 8);
public static String FN_NAMEFIRST = new XFieldInfo
("FirstName",System.String, 32);
public static String FN_NAMELAST = new XFieldInfo
("LastName",System.String, 32);
}
How would I iterate the above to get an array that contains "FirstName", and
"LastName"?
I tried something like (but it didn't work):
public static string[] Fields(){
System.Collections.ArrayList oResult = new
System.Collections.ArrayList();
//It's this line that I think that is hanging me up
System.Type tType =
Test.GetType();//System.Type.GetType("Shema_Customers");
//but this doesn't work either in a static function:
System.Type tType = this.GetType();
System.Reflection.FieldInfo[] tFieldInfos =
tType.GetFields(System.Reflection.BindingFlags.Static);
foreach (System.Reflection.FieldInfo tFieldInfo in tFieldInfos) {
if (tFieldInfo.Name.ToUpper().Substring(0,3) == "FN_") {
oResult.Add(tFieldInfo.GetValue(null));
}
}
return (string[])oResult.ToArray(System.Type.GetType("System.String"));
}
I'm driving myself nuts with trying to get a grip on replacing my SQL calls
with some form of DataObjects...and some of the tricks I used to do by
making SQL on the fly based on info gleamed from schema queries...are
difficult to bring across...
Any and all help greatly appreciated
;-)
Methods:
If I define a class:
class Customers{
public int ID;
public string FirstName;
public string LastName;
}
and I want to get an array of these FieldNames WITHOUT INSTANTIATING THE
CLASS ... (to make SQL from, on the fly)... how do I do this?
Situation II, in case I go a little further...(and don't get lost!) what
about something like:
class XFieldInfo {
public string FieldName ="";
public System.Type FieldType;
public int FieldLength;
}
class Schema_Customer{
public static ID = new XFieldInfo("ID",System.Int32, 8);
public static String FN_NAMEFIRST = new XFieldInfo
("FirstName",System.String, 32);
public static String FN_NAMELAST = new XFieldInfo
("LastName",System.String, 32);
}
How would I iterate the above to get an array that contains "FirstName", and
"LastName"?
I tried something like (but it didn't work):
public static string[] Fields(){
System.Collections.ArrayList oResult = new
System.Collections.ArrayList();
//It's this line that I think that is hanging me up
System.Type tType =
Test.GetType();//System.Type.GetType("Shema_Customers");
//but this doesn't work either in a static function:
System.Type tType = this.GetType();
System.Reflection.FieldInfo[] tFieldInfos =
tType.GetFields(System.Reflection.BindingFlags.Static);
foreach (System.Reflection.FieldInfo tFieldInfo in tFieldInfos) {
if (tFieldInfo.Name.ToUpper().Substring(0,3) == "FN_") {
oResult.Add(tFieldInfo.GetValue(null));
}
}
return (string[])oResult.ToArray(System.Type.GetType("System.String"));
}
I'm driving myself nuts with trying to get a grip on replacing my SQL calls
with some form of DataObjects...and some of the tricks I used to do by
making SQL on the fly based on info gleamed from schema queries...are
difficult to bring across...
Any and all help greatly appreciated
;-)