![]() |
Web Development »
ASP.NET »
General
Intermediate
WebForms Automatic Generation Using Reflection (2)By ediazcThis article continues the series on Web Form automatic generation. |
C#, Windows, Win Mobile, .NET 1.1, ASP.NET, VS.NET2003, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||
In the previous article, I gave you a general idea about this project where we build a form automatically from class generation.
This time we will explore the basics of reflection used in our project.
You can use reflection to get the inside information of a class.
I am not going to teach you all about reflection, there is a lot to discuss about this topic. For more information on this, please go through my blog.
I will show you how to read the public properties of a class and generate a simple form. This is our first experiment on generation and not the final component that we want to build.
First, we will create a class called FormGen. Here is the declaration of this class:
class FormGen {
public static void Generate(Type classType, TextWriter output);
}
You can use this class in ASP.NET in the following way:
class MyForm
{
public string Email;
public string Password;
public string Comments;
}
....
private void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
FormGen.Generate(typeof(MyForm), Response.Output);
}
You can download the implementation of GenForm. This is a basic implementation in our first attempt to explore this technology. Here is the core code to implement a simple reflector form generator:
using System;
using System.IO;
using System.Reflection;
...
public static void Generate(Type classType, TextWriter output)
{
WriteTableHeader(output);
FieldInfo[] fields = classType.GetFields ();
for (int i = 0; i < fields.Length; i++)
{
WriteRow(output, fields[i].Name,
InputText(fields[i].Name));
}
WriteTableFooter(output);
}
This is very limited... In this implementation we have focused only on the text input box.
In the next article, we will use attributes to better handle the format of the form.
Stay tuned .... :)
| You must Sign In to use this message board. | ||||||||
|
||||||||
|
||||||||
|
||||||||
|
||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 24 Jun 2005 Editor: Rinish Biju |
Copyright 2005 by ediazc Everything else Copyright © CodeProject, 1999-2009 Web11 | Advertise on the Code Project |