How to get an Object by it's name?

  • Thread starter Thread starter Michael Stahl
  • Start date Start date
M

Michael Stahl

What I want to do:
I want to have a class which is reading an .ini file and manipulate the
application with the data from the ini file.

What I have now:
An ini file:

[textBox1]
Text=Hallo
Dock=none
ForeColor=red

A class with:

object iwill=null;
parentType = this.GetType();
System.IO.StreamReader sr=
new System.IO.StreamReader(INIFilePath,System.Text.Encoding.UTF8,false);
string str="";
while(sr.Peek()>=0)
{
str=sr.ReadLine().Trim();
//Get the Control I want to change
if(str.StartsWith("["))
{
foreach(Control con in this.Controls)
{
if("["+con.Name.ToString()+"]"==str)
{
iwill=con;
}
}
}
Type myType = iwill.GetType();
//Get the Property I want to change
PropertyInfo pi =myType.GetProperty(str.Split('=')[0]);
if( pi != null )
{
object result=null;
TypeConverter converter=
TypeDescriptor.GetConverter(pi.PropertyType);
result =converter.ConvertFrom(str.Split('=')[1]);
//Change the property
pi.SetValue(iwill,result,null);
}
}

With this i could change textBox1 properties like Dock, Text...
This is working with all my controlls in my Application, but not with
other objects, like sqlConnectionString, or sqlSelectStatements, cause
they are no Controlls.


QUESTION:
Is there a way to get thru all objects in my Application, like i do it
with the Controls in my example?
Must I do something completely different?

Hope someone knows what to do!!
 
Michael said:
What I want to do:
I want to have a class which is reading an .ini file and manipulate the
application with the data from the ini file.

What I have now:
An ini file:

[textBox1]
Text=Hallo
Dock=none
ForeColor=red

A class with:

object iwill=null;
parentType = this.GetType();
System.IO.StreamReader sr=
new System.IO.StreamReader(INIFilePath,System.Text.Encoding.UTF8,false);
string str="";
while(sr.Peek()>=0)
{
str=sr.ReadLine().Trim();
//Get the Control I want to change
if(str.StartsWith("["))
{
foreach(Control con in this.Controls)
{
if("["+con.Name.ToString()+"]"==str)
{
iwill=con;
}
}
}
Type myType = iwill.GetType();
//Get the Property I want to change
PropertyInfo pi =myType.GetProperty(str.Split('=')[0]);
if( pi != null )
{
object result=null;
TypeConverter converter=
TypeDescriptor.GetConverter(pi.PropertyType);
result =converter.ConvertFrom(str.Split('=')[1]);
//Change the property
pi.SetValue(iwill,result,null);
}
}

With this i could change textBox1 properties like Dock, Text...
This is working with all my controlls in my Application, but not with
other objects, like sqlConnectionString, or sqlSelectStatements, cause
they are no Controlls.


QUESTION:
Is there a way to get thru all objects in my Application, like i do it
with the Controls in my example?
Must I do something completely different?

Hope someone knows what to do!!


Reflection should do the trick
 
You could use reflection to go through all *types* in the application maybe but not all objects. No-one (except the runtime) keeps a list of all the allocated objects. If you want to do this you'll have toio store them in a hashtable or something similar. The problem though is that the hashtable will keep the objects alive and so they won't be GC'd so in fact you heed a hash table whose value (of the key/value pair) is a WeakReference and have special handling for eliminating entries with null values.

Regards

RIchard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk


Michael said:
What I want to do:
I want to have a class which is reading an .ini file and manipulate the
application with the data from the ini file.

What I have now:
An ini file:

[textBox1]
Text=Hallo
Dock=none
ForeColor=red

A class with:

object iwill=null;
parentType = this.GetType();
System.IO.StreamReader sr=
new System.IO.StreamReader(INIFilePath,System.Text.Encoding.UTF8,false);
string str="";
while(sr.Peek()>=0)
{
str=sr.ReadLine().Trim();
//Get the Control I want to change
if(str.StartsWith("["))
{
foreach(Control con in this.Controls)
{
if("["+con.Name.ToString()+"]"==str)
{
iwill=con;
}
}
}
Type myType = iwill.GetType();
//Get the Property I want to change
PropertyInfo pi =myType.GetProperty(str.Split('=')[0]);
if( pi != null )
{
object result=null;
TypeConverter converter=
TypeDescriptor.GetConverter(pi.PropertyType);
result =converter.ConvertFrom(str.Split('=')[1]);
//Change the property
pi.SetValue(iwill,result,null);
}
}

With this i could change textBox1 properties like Dock, Text...
This is working with all my controlls in my Application, but not with
other objects, like sqlConnectionString, or sqlSelectStatements, cause
they are no Controlls.


QUESTION:
Is there a way to get thru all objects in my Application, like i do it
with the Controls in my example?
Must I do something completely different?

Hope someone knows what to do!!


Reflection should do the trick
 
voltaire said:
Michael said:
What I want to do:
I want to have a class which is reading an .ini file and manipulate
the

application with the data from the ini file.

What I have now:
An ini file:

[textBox1]
Text=Hallo
Dock=none
ForeColor=red

A class with:

object iwill=null;
parentType = this.GetType();
System.IO.StreamReader sr=
new
System.IO.StreamReader(INIFilePath,System.Text.Encoding.UTF8,false);

string str="";
while(sr.Peek()>=0)
{
str=sr.ReadLine().Trim();
//Get the Control I want to change
if(str.StartsWith("["))
{
foreach(Control con in this.Controls)
{
if("["+con.Name.ToString()+"]"==str)
{
iwill=con;
}
}
}
Type myType = iwill.GetType();
//Get the Property I want to change
PropertyInfo pi =myType.GetProperty(str.Split('=')[0]);
if( pi != null )
{
object result=null;
TypeConverter converter=
TypeDescriptor.GetConverter(pi.PropertyType);
result =converter.ConvertFrom(str.Split('=')[1]);
//Change the property
pi.SetValue(iwill,result,null);
}
}

With this i could change textBox1 properties like Dock, Text...
This is working with all my controlls in my Application, but not with

other objects, like sqlConnectionString, or sqlSelectStatements,
cause

they are no Controlls.


QUESTION:
Is there a way to get thru all objects in my Application, like i do
it

with the Controls in my example?
Must I do something completely different?

Hope someone knows what to do!!



Reflection should do the trick
I tried it with Reflection, and i get the objects, but how would i write
the line
pi.SetValue();
If i put the found object in
pi.SetValue(foundObject,string,null);
i get an
"Object type cannot be converted to target type"
cause foundObject is a Reflection Type not the Object that i want to change
 
Back
Top