Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to do this in .NET
Message
From
15/04/2005 08:05:32
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
15/04/2005 07:56:16
Walter Meester
HoogkarspelNetherlands
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
01004810
Message ID:
01005048
Views:
29
>Hi Cetin,
>
>>If you wouldn't ask for the code now (I believe I posted it partially before here) I did that using System.Reflection.
>
>I thought this would come up. I've heard about the power of reflection. It would be nice though to see how it is used to the specified problem.
>
>>However you ask for the optimal way:) In .Net there are too many classes and too many ways to reach to same endpoint so I'm not sure what I did was the optimal one or not. But also think of this:
>>In .net you can get an xml representation of an object if it's serializable and might quickly process xml to find what you want - just another way.
>>Up till now I couldn't find anything that I can do in VFP but not in .Net (it only takes more time in .Net but that's specific to my relatively much more little knowledge with .Net than it is with VFP).
>
>My main struggle is here how to figure out if a property is available and how to access it, just like you can do in VFP with a simple:
>
>
IF PEMSTATUS(oObject, "MyCustumProperty",5) AND oObject.MyCustumProperty
>
>Needless to say that I'm using such constructs a lot in my framework, and haven't quite found out how you efficiently can translate that into .NET. or actually in a strict typed language.
>
>In fact people hinted me about interfacing, reflection etc, but any way you put it it does not seem to be a full replacement. In fact it seems I have to go back one level and see what I'm trying to achieve up here and find a strategic other solution to this particular problem. To be honest I'm not too fond of using interfaces as then the algorithm has to rely one the interfaces assigned to the target objects, which from a maintenance POV seems a bit problematic to me (I might be wrong though).
>
>Reflection is something I though might be a solution, but do see this as a .NET implementation rather as a solution to other static typed languages. Besides, it seems that reflection is some mechanism that bypasses the static type checking, which really was the original question in the discussion where this started.
>
>
>>PS: I used GetType() and Type class members like MemberInfo, FieldInfo, PropertyInfo etc instead of typeof() so I needn't know the type beforehand and create a switch for every possible new object. It looks like amembers() processing in VFP. Off topic I hate in VFP I need Pages/PageCount, Buttons/ButtonCount etc (yes there are Objects but still I hate:)
>
>I agree those properties should not have been create in the first place...
>
>Thanks,
>
>Walter,

Walter,
It's impossible to locate one matching that right away but this is what I could find quickly (a simple reflection test code). I don't remember why parts were commented (I generally do it with test codes even if that part works to test different things):
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Reflection;

namespace ReflectionTest
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.TextBox textBox4;
		private System.Windows.Forms.TextBox textBox5;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.TextBox textBox;
		private System.Windows.Forms.TextBox SomeTextBox;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.TextBox textBox6;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.textBox4 = new System.Windows.Forms.TextBox();
			this.textBox5 = new System.Windows.Forms.TextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.textBox = new System.Windows.Forms.TextBox();
			this.SomeTextBox = new System.Windows.Forms.TextBox();
			this.panel1 = new System.Windows.Forms.Panel();
			this.textBox6 = new System.Windows.Forms.TextBox();
			this.panel1.SuspendLayout();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(0, 0);
			this.label1.Name = "label1";
			this.label1.TabIndex = 0;
			this.label1.Text = "label1";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(0, 32);
			this.label2.Name = "label2";
			this.label2.TabIndex = 1;
			this.label2.Text = "label2";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(0, 64);
			this.label3.Name = "label3";
			this.label3.TabIndex = 2;
			this.label3.Text = "label3";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(0, 96);
			this.label4.Name = "label4";
			this.label4.TabIndex = 3;
			this.label4.Text = "label4";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(0, 128);
			this.label5.Name = "label5";
			this.label5.TabIndex = 4;
			this.label5.Text = "label5";
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(120, 0);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(152, 20);
			this.textBox1.TabIndex = 5;
			this.textBox1.Text = "textBox1";
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(120, 32);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(152, 20);
			this.textBox2.TabIndex = 6;
			this.textBox2.Text = "textBox2";
			// 
			// textBox3
			// 
			this.textBox3.Location = new System.Drawing.Point(120, 56);
			this.textBox3.Name = "textBox3";
			this.textBox3.Size = new System.Drawing.Size(152, 20);
			this.textBox3.TabIndex = 7;
			this.textBox3.Text = "textBox3";
			// 
			// textBox4
			// 
			this.textBox4.Location = new System.Drawing.Point(120, 88);
			this.textBox4.Name = "textBox4";
			this.textBox4.Size = new System.Drawing.Size(152, 20);
			this.textBox4.TabIndex = 8;
			this.textBox4.Text = "textBox4";
			// 
			// textBox5
			// 
			this.textBox5.Location = new System.Drawing.Point(120, 120);
			this.textBox5.Name = "textBox5";
			this.textBox5.Size = new System.Drawing.Size(152, 20);
			this.textBox5.TabIndex = 9;
			this.textBox5.Text = "textBox5";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(192, 224);
			this.button1.Name = "button1";
			this.button1.TabIndex = 10;
			this.button1.Text = "Change texts";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// textBox
			// 
			this.textBox.Location = new System.Drawing.Point(128, 152);
			this.textBox.Name = "textBox";
			this.textBox.TabIndex = 11;
			this.textBox.Text = "textBox";
			this.textBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox_KeyPress);
			// 
			// SomeTextBox
			// 
			this.SomeTextBox.Location = new System.Drawing.Point(56, 184);
			this.SomeTextBox.Name = "SomeTextBox";
			this.SomeTextBox.TabIndex = 12;
			this.SomeTextBox.Text = "Sometextbox";
			// 
			// panel1
			// 
			this.panel1.Controls.Add(this.textBox6);
			this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.panel1.Location = new System.Drawing.Point(0, 410);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(568, 100);
			this.panel1.TabIndex = 13;
			// 
			// textBox6
			// 
			this.textBox6.Location = new System.Drawing.Point(48, 32);
			this.textBox6.Name = "textBox6";
			this.textBox6.TabIndex = 0;
			this.textBox6.Text = "textBox6";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(568, 510);
			this.Controls.Add(this.panel1);
			this.Controls.Add(this.SomeTextBox);
			this.Controls.Add(this.textBox);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.textBox5);
			this.Controls.Add(this.textBox4);
			this.Controls.Add(this.textBox3);
			this.Controls.Add(this.textBox2);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.label5);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.panel1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		
		private void SetUsingReflection(int start,int end,string prefix)
		{
			for(int i=start;i<=end;i++)
			{
				string cMember = "textBox"+i.ToString();
				TextBox member = (TextBox)this.GetType().InvokeMember(cMember,
					BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.GetField,
					null,this,new object [] {});
				member.Text = prefix+i.ToString();
			}
		}

		private Control FindObjectByCollection(string objectName)
		{
			foreach(System.Windows.Forms.Control ctl in this.Controls)
			{
				if (ctl.Name.IndexOf(objectName) == 0)
					return ctl;
			}
			return null;
		}

		private Control FindObjectByReflection(string objectName)
		{
			try
			{
				Object o = this.GetType().InvokeMember(objectName,
					BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.GetField,
					null,this,new object [] {});
				return (Control)o;
			}
			catch
			{
				return null;
			}
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
/*
  			for(int i=1;i<=3;i++)
			{
				Control ctl = FindObjectByCollection("textBox"+i.ToString());
				if ( ctl != null )
					ctl.Text="Hello "+i.ToString();
			}
			for(int i=4;i<=6;i++)
			{
				Control ctl = FindObjectByReflection("textBox"+i.ToString());
				if ( ctl != null )
					ctl.Text="Hello "+i.ToString();
			}
*/
			//FindObjectByCollection("textBox4").Text="Hello 4";
			//FindObjectByReflection("textBox3").Text="Hello 3";
			//SetUsingControlsCollection(1,3,"Controls");
			//SetUsingReflection(4,6,"Reflection");

			//MemberInfo[] arrayMemberInfo = myForm.FindMembers(MemberTypes.All,
			//	BindingFlags.NonPublic | BindingFlags.Instance,
			//	new MemberFilter(DelegateToSearchCriteria),
			//	cMember);
		}

		private void textBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			if (Control.ModifierKeys == Keys.Control && e.KeyChar == 22)
			{
				e.Handled = true;
				((Control)sender).Text = "Ctrl+V pressed";
			}
		}
	}
}
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Reply
Map
View

Click here to load this message in the networking platform