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

Reflection, FieldInfo and ref

Hi,

I'm trying to do something with reflection that's a little wierd. The idea
is that I call for a field to be updated on a base class. Using reflection
it first updates the value, and then updates the change that has been made
to a dictionary. The code below is just to encourage some ideas, if you look
at most of the code you find that most of it is pretty pointless. I might as
well have Translate(string fieldname, ref string objecttotranslate) as the
method and call the fielddictionary directly.

What I'm really looking to do though, is to get rid of the 'string
fieldname' and simplify the method so that it is simply 'Translate(ref
string field)'

The problem is that I can seem to find anyway to get the field 'ref string
field' is refering to, and even if I could I'm not sure how I could use
reflection to then get the name of the field that was passed by reference. I
suppose one solution would be to iterate through Type.GetFields() and
compare values, but does anyone know of a better solution?

Thanks and kind regards

James

using System;
using System.Reflection;
using System.Collections.Specialized;
using System.Diagnostics;
public class BaseClass
{
private HybridDictionary fielddictionary = new HybridDictionary();
public BaseClass()
{
FieldInfo fi =
this.GetType().GetField("helloworld",
BindingFlags.NonPublic | BindingFlags.Instance);
fielddictionary.Add(fi.Name, fi.GetValue(this));
}
protected void Translate(FieldInfo fieldinfo)
{
this.Translate(fieldinfo.FieldHandle);
}
protected void Translate(RuntimeFieldHandle handle)
{
FieldInfo fi = FieldInfo.GetFieldFromHandle(handle);
fi.SetValue(this, "Hola Mundo");
fielddictionary[fi.Name] = "Hola Mundo";
}
}
public class SubClass :BaseClass
{
private string helloworld = "Hello World";
internal string HelloWorld{get{return helloworld;}}
public SubClass(){}
internal void Translate()
{
// RuntimeFieldHandle handle =
// this.GetType().GetField("helloworld",
// BindingFlags.NonPublic |
BindingFlags.Instance).FieldHandle;
// base.Translate(handle);
FieldInfo fieldinfo = this.GetType().GetField("helloworld",
BindingFlags.NonPublic | BindingFlags.Instance);
base.Translate(fieldinfo);
}
}
public class Test
{
public static int Main()
{
SubClass test = new SubClass();
Debug.WriteLine(test.HelloWorld);
test.Translate();
Debug.WriteLine(test.HelloWorld);
return 0;
}
}
Nov 16 '05 #1
0 2132

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

Similar topics

2
by: ichor | last post by:
hi i have read a few examples that use the System.Reflection api , but i cant understand in what way is it helpful. i mean its practical use in the real world thanx
3
by: Ken Durden | last post by:
Hey, I posted this a few months ago and got completely off-the-chart responses. At the time I wasn't using reflection very much, but more and more I find myself wanting to use it to write...
7
by: maf | last post by:
Using reflection, I'm trying to get the value for a constant in an enum. Getting the contant name works fine using: FieldInfo fieldInfos = TYPE.GetFields(); foreach(FieldInfo fi in fieldInfos...
1
by: Freddy Willockx | last post by:
Hi I'm doing some actions through reflection and generating some source code to speed up my actions. But, when I'm generating source code to read and write the fields of an Enum type, it goes...
5
by: Eric Goforth | last post by:
Hello, I have a generic subroutine that I pass an object and fieldname as arguments. The subroutine then uses reflection to search for the value of the fieldname. For example: 'Calling...
2
by: André | last post by:
I try to initialize some Public Variable in a class use for code-behind The idea is to check all uxMsgXXX variables, declare in top of my class and assign the string value from a DB request. Why...
5
by: glayos | last post by:
Hi all, i have a question about the framework reflection. I have a project with some pages and a component class file. Inside this file I've placed some custom objects. Using the reflection and...
2
by: Torben Laursen | last post by:
I have a complicated class that I use to store a lot of values Now I need a copy of that class and I have been trying to find a way to do this automatic so I don't have to assign all properties and...
6
by: chandramohanp | last post by:
Hi I am trying to modify class instance members using reflection. I am having problem when trying to add/remove/display elements related to List<int> member. Following is the code. class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.