473,387 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Retrieving Class Information - Reflection??

Hi. How can I use reflection to determine what members are in my own class? I have tried this code below but it doesn't display "MyPublicString".

Somebody help me please?

Expand|Select|Wrap|Line Numbers
  1. using System.Reflection;
  2.  
  3. namespace ClassInterrogate
  4. {
  5.   public class MyClass
  6.   {
  7.     public  String MyPublicString;
  8.     private String MyPrivateString;
  9.   }
  10.  
  11.   class Program
  12.   {
  13.     static void Main( string[] args )
  14.     {
  15.       MyClass        m = new MyClass();
  16.       Type           baseType;
  17.       MemberInfo  [] publicMembers;
  18.  
  19.       baseType  = m.GetType().BaseType;
  20.  
  21.       publicMembers = baseType.GetMembers();
  22.  
  23.        foreach( MemberInfo member in publicMembers )
  24.       {
  25.         Console.WriteLine( "Name:{0}", member.Name );
  26.        }
  27.     }
  28.   }
  29. }
  30.  
Jul 25 '07 #1
1 1004
DOHH! DOHH!! DOHH!!!! Here's the answer.. I hope this helps somebody else. Amazing how I can't figure something out until I post a question! :-) :-)

Yay!!!

Expand|Select|Wrap|Line Numbers
  1. namespace ClassInterrogate
  2. {
  3.   public class MyClass
  4.   {
  5.     public  String MyPublicString;
  6.     private String MyPrivateString;
  7.   }
  8.  
  9.   class Program
  10.   {
  11.     static void Main( string[] args )
  12.     {
  13.       MyClass        m = new MyClass();
  14.       Type           baseType;
  15.       MemberInfo  [] publicMembers;
  16.  
  17.       baseType  = m.GetType();  // <---- This is why previous didn't work
  18.  
  19.       publicMembers = baseType.GetMembers();
  20.  
  21.       foreach( MemberInfo member in publicMembers )
  22.       {
  23.         Console.WriteLine( "Name:{0}", member.Name );
  24.       }
  25.     }
  26.   }
  27. }
  28.  
Jul 25 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Ray Z | last post by:
hi, there I build a Class Library, I write a class A to implement interface IA. The problem is when I give the dll file to others, thet can get all information about not only IA, but also A. They...
2
by: Rob | last post by:
What VB run-time methods are available to retrieve information (such as AssemblyCopyright, AssemblyProduct, AssemblyVersion, etc.) from the AssemblyInfo.vb file for the current executable? ...
7
by: clr | last post by:
I like to stamp trace logs with the name of the executing Class and Method. I can get the Class Name using GetType.Name and I can get a list of every Method in the class using...
3
by: Fredrik Strandberg | last post by:
Hi! I have some problems when testing a system. In class A i have: Public Event CommEvent As CommEventHandler Public Delegate Sub CommEventHandler() and a method:
2
by: Sakke | last post by:
Hello! We have written a GCryptoSvr.dll COM server in C++. Inside that resides WebClient COM component. WebClient CLSID is {8DC27D48-F94C-434B-A509-C3E1A3E75B9E}. When we are using that...
1
by: jimmyfo | last post by:
Hi, I recently wrote an ASP.Net web application in VS2005 and published (using VS2005 Publish feature) it to a relatively clean machine with ASP.Net 2.0 and MDAC 2.8 installed on it. However, when...
3
by: Goran Djuranovic | last post by:
Hi all, Is there a way to retrieve a derived class name inside a subroutine or a function of the base class? I am trying to get some data from the database, based on which derived class is calling...
1
by: mary mathews | last post by:
Hi, I'm getting an exception while tried to retrive an outlook contact using asp.net in a console application. The exception is as follows: An unhandled exception of type...
1
by: Olaf Rabbachin | last post by:
Hi folks, I'd like to retrieve default values for properties (of a user control) via code instead of having string-constants. Consider the following (simplified) property: Private _MyColor...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.