dotnetspider.com


 


TutorialsForumResourcesReviewsJobsInterviewVideosCommunitiesProjectsTraining

Subscribe to Subscribers


Online MembersSankar
Sunitha
Ashokkumar
Anu George
chanti
Falguni
Prachi Kulkarni
cloud
Prasad kulkarni
More...




Resources » Code Snippets » Reflection


C#:Reflection - A Step Ahead Series


Posted Date:     Category: Reflection    Rating: 1 out of 5
Author: Member Level: Gold    Points: 10 (Rs 10)


C#:Reflection - A Step Ahead Series


Introduction:


This is a continuation to my earlier post C#-The Language : A Step Ahead Series-Part-II
Simply, the mechanism through which one can access metadata information is called reflection. Namespace System.Reflection can be used to browse through the metadata information. Using reflection you can also dynamically invoke methods using System.Type.Invokemember.


/* This Example is a part of different
* examples shown in Book:
* C#2005 Beginners: A Step Ahead
* Written by: Gaurav Arora
* Reach at : msdotnetheaven*/

// File name : reflectionAssembly.cs

using System;
using System.Reflection;
namespace CSharp.AStepAhead.reflectionAssembly
{
public class reflectionAssembly
{
public static void Main()
{
Type myType = typeof(aClassToTest);
Console.Clear();
Console.WriteLine("\nType of class: " + myType);
Console.WriteLine("Namespace: " + myType.Namespace);
ConstructorInfo[] ci = myType.GetConstructors();
Console.WriteLine("\n-----------------------------------------------");
Console.WriteLine("Constructors are:");
foreach (ConstructorInfo i in ci)
{
Console.WriteLine(i);
}
PropertyInfo[] pi = myType.GetProperties();
Console.WriteLine("\n-----------------------------------------------");
Console.WriteLine("Properties are:");
foreach (PropertyInfo i in pi)
{
Console.WriteLine(i);
}
MethodInfo[] mi = myType.GetMethods();
Console.WriteLine("\n-----------------------------------------------");
Console.WriteLine("Methods are:");
foreach (MethodInfo i in mi)
{
Console.WriteLine("\nName: " + i.Name);
ParameterInfo[] pif = i.GetParameters();
foreach (ParameterInfo p in pif)
{
Console.WriteLine(" Type: " + p.ParameterType + " parameter name: " + p.Name);
}
}
Console.ReadLine();
}

public class aClassToTest
{
public int pubInteger;
private int _privValue;

public aClassToTest()
{
//Blank construtor
}

public aClassToTest(int newIntValue)
{
pubInteger = newIntValue;
}
public int Add(int num1, int num2)
{
return num1 + num2;
}
public int Substract(int num1, int num2)
{
return num1 - num2;
}
public int Multiply(int num1, int num2)
{
return num1 * num2;
}

public int privValue
{
get
{
return _privValue;
}
set
{
_privValue = value;
}
}
}
}
}



In above example, by simply specifying the name of a type, you can query almost all aspects of the object.
You can determine whether the object is a class, what its base system type is, and many other properties.


Did you like this resource? Share it with your friends and show your love!





Responses to "C#:Reflection - A Step Ahead Series"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Copy all control values of web page to get set properties
    Previous Resource: Code Dom: A Step Ahead Series
    Return to Resources
    Post New Resource
    Category: Reflection


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    (No tags found.)

    My Profile

    Active Members
    TodayLast 7 Daysmore...


    Awards & Gifts


    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds



    About Us    Trademark Disclaimer    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.