Click here to Skip to main content
15,885,954 members

Alex Perepletov - Professional Profile



Summary

    Blog RSS
3,367
Author
179
Authority
12
Debator
24
Editor
10
Organiser
133
Participant
0
Enquirer
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Reputation

Weekly Data. Recent events may not appear immediately. For information on Reputation please see the FAQ.

Privileges

Members need to achieve at least one of the given member levels in the given reputation categories in order to perform a given action. For example, to store personal files in your account area you will need to achieve Platinum level in either the Author or Authority category. The "If Owner" column means that owners of an item automatically have the privilege. The member types column lists member types who gain the privilege regardless of their reputation level.

ActionAuthorAuthorityDebatorEditorEnquirerOrganiserParticipantIf OwnerMember Types
Have no restrictions on voting frequencysilversilversilversilver
Bypass spam checks when posting contentsilversilversilversilversilversilvergoldSubEditor, Mentor, Protector, Editor
Store personal files in your account areaplatinumplatinumSubEditor, Editor
Have live hyperlinks in your profilebronzebronzebronzebronzebronzebronzesilverSubEditor, Protector, Editor
Have the ability to include a biography in your profilebronzebronzebronzebronzebronzebronzesilverSubEditor, Protector, Editor
Edit a Question in Q&AsilversilversilversilverYesSubEditor, Protector, Editor
Edit an Answer in Q&AsilversilversilversilverYesSubEditor, Protector, Editor
Delete a Question in Q&AYesSubEditor, Protector, Editor
Delete an Answer in Q&AYesSubEditor, Protector, Editor
Report an ArticlesilversilversilversilverSubEditor, Mentor, Protector, Editor
Approve/Disapprove a pending ArticlegoldgoldgoldgoldSubEditor, Mentor, Protector, Editor
Edit other members' articlesSubEditor, Protector, Editor
Create an article without requiring moderationplatinumSubEditor, Mentor, Protector, Editor
Approve/Disapprove a pending QuestionProtector
Approve/Disapprove a pending AnswerProtector
Report a forum messagesilversilverbronzeProtector, Editor
Approve/Disapprove a pending Forum MessageProtector
Have the ability to send direct emails to members in the forumsProtector
Create a new tagsilversilversilversilver
Modify a tagsilversilversilversilver

Actions with a green tick can be performed by this member.


 
RantControl.GetStyle() should be public. Pin
Alex Perepletov15-Mar-09 19:52
Alex Perepletov15-Mar-09 19:52 
GeneralRetrieve PropertyInfo of current property. Pin
Alex Perepletov28-Dec-08 5:35
Alex Perepletov28-Dec-08 5:35 
Many properties of my project perform input validation. Exceptions are thrown when input is invalid. I decided to automate generation of the error message that gets displayed in case of these exceptions. The very least that I need to know to display this message is the name of the property that threw an exception.

At first, I changed set accessors of some properties to throw property name as the Exception message. Like so:

public int MyIntProperty
{
	get { return _myInt; }
	set
	{
		if (value == 10)
			throw new Exception("MyIntProperty");
		_myInt = value;
	}
}


This has a drawback of manually updating the string that gets thrown. One can forget to do so when changing property name, or make a typo. Can’t we make computer do this for us? How can we find information on property from inside that property?

Information on currently executing method is found using static method MethodBase.GetCurrentMethod(). Too bad there is no PropertyInfo.GetCurrentProperty() method. That is basically what we want.

MethodBase.GetCurrentMethod() still comes to the rescue. Compiler transforms get accessors into get_PropertyName methods, while set accessors are transformed into set_PropertyName methods. Calling MethodBase.GetCurrentMethod() from the set accessor of the MyIntProperty above returns a setAccessor = {Void set_MyIntProperty(Int32)}

So, we have the set accessor method. Now all we need to do is to loop through Properties of our object’s Type, and find the Property that has set_MyIntProperty set accessor. PropertyInfo.GetSetMethod() looks up the Property set accessor function. The following function finds the PropertyInfo for a specific set accessor:

private PropertyInfo FindMyProperty(object obj, MethodBase setAccessor)
{
	Type type = obj.GetType();
	foreach (PropertyInfo pi in type.GetProperties())
	{
		MethodInfo mi = pi.GetSetMethod();
		if (MethodInfo.Equals(mi, setAccessor))
		{
			return pi;
		}
	}
	return null;
}


Here’s an example of a modified MyIntProperty that uses the new function to find its own PropertyInfo:

using System;
using System.Reflection;

namespace CurrentProperty
{
	class Program
	{
		private float _myFloat = 0f;
		private int _myInt = 0;

		static void Main(string[] args)
		{
			Program p = new Program();

			p.MyFloatProperty = 10f;
			p.MyIntProperty = 10;
		}

		public float MyFloatProperty
		{
			get { return _myFloat; }
			set
			{
				if (value == 10f)
					throw new Exception(FindMyProperty(this, MethodBase.GetCurrentMethod()).Name);

				_myFloat = value;
			}
		}

		public int MyIntProperty
		{
			get { return _myInt; }
			set
			{
				if (value == 10)
					throw new Exception(FindMyProperty(this, MethodBase.GetCurrentMethod()).Name);

				_myInt = value;
			}
		}

		private PropertyInfo FindMyProperty(object obj, MethodBase setAccessor)
		{
			Type type = obj.GetType();
			foreach (PropertyInfo pi in type.GetProperties())
			{
				MethodInfo mi = pi.GetSetMethod();
				if (MethodInfo.Equals(mi, setAccessor))
				{
					return pi;
				}
			}
			return null;
		}
	}
}

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.