473,396 Members | 1,975 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,396 software developers and data experts.

Reflection, how to?

If I have a Class below and I want to use Reflection to get classes
member variable value how should I actually do it? Can anyone help?

Cheers!

using System;
using System.Reflection;

namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class.
/// </summary>
class MyClass
{
private string MyValue = String.Empty;
public void setMyValue(string s)
{
MyValue = s;
}

[STAThread]
static void Main(string[] args)
{
MyClass o = new MyClass();
o.setMyValue("Value is String");

// How to get varaible MyValu's value
// using Reflection????
}
}
}
May 18 '06 #1
2 1199
Hello James,

See msdn sample http://msdn2.microsoft.com/en-us/library/ch9714z3.aspx

JB> If I have a Class below and I want to use Reflection to get classes
JB> member variable value how should I actually do it? Can anyone help?
JB>
JB> Cheers!
JB>
JB> using System;
JB> using System.Reflection;
JB> namespace ConsoleApplication1
JB> {
JB> /// <summary>
JB> /// Summary description for Class.
JB> /// </summary>
JB> class MyClass
JB> {
JB> private string MyValue = String.Empty;
JB> public void setMyValue(string s)
JB> {
JB> MyValue = s;
JB> }
JB> [STAThread]
JB> static void Main(string[] args)
JB> {
JB> MyClass o = new MyClass();
JB> o.setMyValue("Value is String");
JB> // How to get varaible MyValu's value
JB> // using Reflection????
JB> }
JB> }
JB> }
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
May 18 '06 #2
I m explaining it below

Below is data of my assembly

string ModuleName = "TestAssembly.dll";
string TypeName = "TestClass";
string MethodName = "TestMethod";
Loading my assembly using
Assembly myAssembly = Assembly.LoadFrom(ModuleName);
After that three loops are used to inspect the assembly and get the chosen
module/type/method combination. Within the last loop the type is instanciated
and the method is called. The method can be called with parameters, but here
we
choose <I>null </I>for methods with no parameters. There can also be a
response
from the method, which we put into a general object.</P><PRE lang=cs>Module
[] myModules = myAssembly.GetModules();
foreach (Module Mo in myModules)
{
if (Mo.Name == ModuleName)
{
Type[] myTypes = Mo.GetTypes();
foreach (Type Ty in myTypes)
{
if (Ty.Name == TypeName)
{
MethodInfo[] myMethodInfo = Ty.GetMethods(flags);
foreach(MethodInfo Mi in myMethodInfo)
{
if (Mi.Name == MethodName)
{
Object obj = Activator.CreateInstance(Ty);
Object response = Mi.Invoke(obj, null);
}
}
}
}
}
}
"James B" wrote:
If I have a Class below and I want to use Reflection to get classes
member variable value how should I actually do it? Can anyone help?

Cheers!

using System;
using System.Reflection;

namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class.
/// </summary>
class MyClass
{
private string MyValue = String.Empty;
public void setMyValue(string s)
{
MyValue = s;
}

[STAThread]
static void Main(string[] args)
{
MyClass o = new MyClass();
o.setMyValue("Value is String");

// How to get varaible MyValu's value
// using Reflection????
}
}
}

May 18 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: A. Wiebenga | last post by:
Hi all! I am a student at the Hogeschool van Arnhem en Nijmegen in Holland. I am currently involved in a research project regarding Reflection. Purpose of the research project is to document...
10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
2
by: Jason Coyne Gaijin42 | last post by:
I have seen several people looking for a way to access the Columns collection when using the AutoGenerate = true option. Some people have gotten so far as to find the private autoGenColumnsArray...
2
by: Mark | last post by:
Am I out of my mind if I use Reflection everytime someone logs into our site to get and track the current Major/Minor/Build/Revision version that the person is viewing our site through? This...
0
by: Shawn Hogan | last post by:
Hi everyone, I've been trying to execute a control's private event code via reflection from another class with the goal of potentially doing some unit testing. The examples below are trying to...
3
by: HL | last post by:
The requirement is to send some information to other objects. The objects to whom the information has to be sent is not available at compile time. The names of the types (objects) will be provided...
9
by: Kuberan Naganathan | last post by:
Hello all Does anyone know of a good way to use reflection in c++? I don't mean simply using rtti or dynamic casting. I'm talking about java/c# style reflection where an actual instance of...
17
by: raylopez99 | last post by:
What good is C# Reflection, other than to find out what types are in an assembly? And to dynamically invoke methods in an assembly (.dll or .exe)? Also, bonus question, can you use Reflection...
0
by: Gustavo Arriola | last post by:
Hola a todos! Estoy intentando ejecutar un método usando Reflection. El código es el siguiente: public static void SoapHandler(Exception Error) { try { Type assemblyType;
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.