Reader Level:
ARTICLE

Working with Reflection

On July 05, 2001 Articles | C# Programming
This article is about the use of reflection package in Csharp. Through Reflection we can get Information about the class.
  • 0
  • 0
  • 15779
Download Files:
 

Introduction

This article is about the use of reflection package in Csharp. Through Reflection we can get Information about the class. It is very useful in designing COM ..we don't have to remember the method name and all other related information regarding class ,we can get it through reflection package.

About Program

This program contain one class Reflect and other one is TCPReflection. Through TCPReflection we are getting information about Reflect class in TCPReflection. In TCPReflection we are getting information member information,Field information and method information of Reflect class. I am giving you this program as simple as possible to understand you can do this with available classes of Csharp also like TCPClient etc.

Compile : csc TCPReflection.cs
execute : TCPReflection

Source Code

using System.Reflection;
using System;
class Reflect
{
public int i=20;
public char ch='a';
public float f1=10;
public void GetFloat()
{
Console.WriteLine(f1);
}
public void GetInt()
{
Console.WriteLine(i);
}
public void GetChar()
{
Console.WriteLine(ch);
}
}
class TCPReflection
{
public void GetInfo()
{
Reflect sender =
new Reflect();
Type t = sender.GetType();
MemberInfo [] members = t.GetMembers();
MethodInfo [] method = t.GetMethods();
FieldInfo [] f1=t.GetFields();
Console.WriteLine("Name of Object "+t.FullName);
Console.WriteLine("no of memeber:"+members.Length);
Console.WriteLine("no of fileds:"+f1.Length);
Console.WriteLine("Member of class Reflect is as follows:" );
foreach(MemberInfo m in members)
{
Console.WriteLine(m.Name);
}
Console.WriteLine("Methods of class Reflect is as follows:" );
foreach(MethodInfo m in method)
{
Console.WriteLine(m.Name);
}
Console.WriteLine("Fields of class Reflect is as follows:" );
foreach(FieldInfo f in f1)
{
Console.WriteLine(f.Name);
}
}
public static void Main()
{
TCPReflection p1 =
new TCPReflection();
p1.GetInfo();
}
}

shrijeet

Shrijeet Nair holds MCA (Master's in Computer Science and Applications) degree. Presently he is working with Netdecisions India Pvt Ltd, Mumbai as a software engineer. He has good hands on experience ...

  • 385rank
  • 552K readers
COMMENT USING

Trending up