By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,490 Members | 1,335 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,490 IT Pros & Developers. It's quick & easy.

How to get an Object by it's name?

P: n/a
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.Enc oding.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!!
Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

Michael Stahl wrote:
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.Enc oding.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

Nov 16 '05 #2

P: n/a
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 Stahl wrote:
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.Enc oding.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
Nov 16 '05 #3

P: n/a
voltaire schrieb:
Michael Stahl wrote:
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.Enc oding.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
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.