Click here to Skip to main content
15,888,979 members
Home / Discussions / C#
   

C#

 
GeneralRe: How can I get the name of the object from Reflection ? Pin
Nadia Monalisa18-Aug-10 4:04
Nadia Monalisa18-Aug-10 4:04 
GeneralRe: How can I get the name of the object from Reflection ? Pin
Łukasz Nowakowski18-Aug-10 4:51
Łukasz Nowakowski18-Aug-10 4:51 
AnswerRe: How can I get the name of the object from Reflection ? Pin
PIEBALDconsult18-Aug-10 3:18
mvePIEBALDconsult18-Aug-10 3:18 
GeneralRe: How can I get the name of the object from Reflection ? Pin
Nadia Monalisa18-Aug-10 3:42
Nadia Monalisa18-Aug-10 3:42 
GeneralRe: How can I get the name of the object from Reflection ? Pin
PIEBALDconsult18-Aug-10 15:09
mvePIEBALDconsult18-Aug-10 15:09 
AnswerRe: How can I get the name of the object from Reflection ? Pin
ddecoy18-Aug-10 4:13
ddecoy18-Aug-10 4:13 
GeneralRe: How can I get the name of the object from Reflection ? Pin
ddecoy18-Aug-10 4:16
ddecoy18-Aug-10 4:16 
AnswerRe: How can I get the name of the object from Reflection ? Pin
Chris Trelawny-Ross20-Aug-10 9:16
Chris Trelawny-Ross20-Aug-10 9:16 
You need to use Linq Expression objects to get the name of a property, from the property itself. Here's a simple test program that demonstrates what you have to do:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Reflection;

namespace ConsoleApplication4
{
class Program
	{
	static void Main(string[] args)
		{
		Consumer c = new Consumer();
		c.TestIt();
		}
	}

public class Demo
	{
	public int InstanceProperty { get; set; }
	public string InstanceField;
	static public int TypeField;
	static public int TypeProperty { get; set; }
	}

public class Consumer
	{
	public void TestIt()
		{
		button1_click(this, new EventArgs());
		}


	private void button1_click(object sender, EventArgs e)
		{
		Demo myDemo = new Demo { InstanceProperty = 10, InstanceField = "Kab" };
		string n1 = GetMemberName(() => myDemo.InstanceField);
		string n2 = GetMemberName(() => myDemo.InstanceProperty);
		string n3 = GetMemberName(() => Demo.TypeField);
		string n4 = GetMemberName(() => Demo.TypeProperty);
		string n5 = GetMemberName(() => myDemo);	// This was a surprise - I expected it to fail!
		}

	private string GetMemberName<T>(Expression<Func<T>> itemSpecifierExpression)
		{
		var bodyAsMemberExpression = itemSpecifierExpression.Body as MemberExpression;
		if (bodyAsMemberExpression == null)
			throw new ArgumentException("itemSpecifierExpression does not specify an object or an instance or type member");
		var propInfo = bodyAsMemberExpression.Member as MemberInfo;
		return bodyAsMemberExpression.Member.Name; 
		}

	}

}
Note - it may be possible to simplify things a bit, but I've not used Expression enough to be sure.

See INotifyPropertyChanging[^] by atverma[^] for the article where I first saw the technique used.
Question555555555555 Pin
winssss17-Aug-10 23:37
winssss17-Aug-10 23:37 
AnswerRe: 555555555555 Pin
Thomas Krojer18-Aug-10 1:24
Thomas Krojer18-Aug-10 1:24 
AnswerRe: 555555555555 Pin
Sauro Viti18-Aug-10 3:43
professionalSauro Viti18-Aug-10 3:43 
QuestionWCF host instant and open programmatically - how to relate to binding in app.config? Pin
1eyhk117-Aug-10 23:30
1eyhk117-Aug-10 23:30 
Questioncharacter postion replacemenet Pin
annie_bel17-Aug-10 23:28
annie_bel17-Aug-10 23:28 
AnswerRe: character postion replacemenet Pin
Gonzalo Cao17-Aug-10 23:51
Gonzalo Cao17-Aug-10 23:51 
GeneralRe: character postion replacemenet Pin
ps_prakash0218-Aug-10 4:27
ps_prakash0218-Aug-10 4:27 
QuestionDataSet does not support System.Nullable<> Pin
treuveni17-Aug-10 23:24
treuveni17-Aug-10 23:24 
AnswerRe: DataSet does not support System.Nullable Pin
Bernhard Hiller18-Aug-10 4:24
Bernhard Hiller18-Aug-10 4:24 
AnswerRe: DataSet does not support System.Nullable Pin
Madmaximus7-Jul-11 4:48
Madmaximus7-Jul-11 4:48 
QuestionSD Card Pin
dataminers17-Aug-10 22:21
dataminers17-Aug-10 22:21 
AnswerRe: SD Card Pin
R. Giskard Reventlov17-Aug-10 22:32
R. Giskard Reventlov17-Aug-10 22:32 
AnswerRe: SD Card Pin
Thomas Krojer18-Aug-10 1:25
Thomas Krojer18-Aug-10 1:25 
QuestionBit Shifting problem Pin
norjali17-Aug-10 21:15
norjali17-Aug-10 21:15 
AnswerRe: Bit Shifting problem Pin
DaveyM6917-Aug-10 21:25
professionalDaveyM6917-Aug-10 21:25 
GeneralRe: Bit Shifting problem Pin
norjali17-Aug-10 21:28
norjali17-Aug-10 21:28 
AnswerRe: Bit Shifting problem Pin
Richard MacCutchan17-Aug-10 21:26
mveRichard MacCutchan17-Aug-10 21:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.