Click here to Skip to main content
15,903,201 members
Home / Discussions / C#
   

C#

 
GeneralRe: Gettings other page data. Pin
Polis Pilavas26-Apr-05 23:40
Polis Pilavas26-Apr-05 23:40 
QuestionHow to pass parameter from C# to a autoit script Pin
Abijawan26-Apr-05 18:08
Abijawan26-Apr-05 18:08 
GeneralEmbedding image file in Assembly Pin
Anonymous26-Apr-05 18:07
Anonymous26-Apr-05 18:07 
GeneralRe: Embedding image file in Assembly Pin
Polis Pilavas26-Apr-05 23:43
Polis Pilavas26-Apr-05 23:43 
GeneralRe: Embedding image file in Assembly Pin
Kodanda Pani26-Apr-05 23:46
Kodanda Pani26-Apr-05 23:46 
QuestionHow to pass parameter from C# to a autoit script Pin
Abijawan26-Apr-05 18:06
Abijawan26-Apr-05 18:06 
GeneralCrystal Reports via ADO.NET Pin
SignMan35926-Apr-05 14:30
SignMan35926-Apr-05 14:30 
QuestionHow do we use PropertyInfo when we don't know data type? Pin
rudy.net26-Apr-05 13:57
rudy.net26-Apr-05 13:57 
I wrote a class that binds a specific property of a control to Properties in another class. For example, given the class below:
<br />
public class MyClass<br />
{<br />
  public int MyProperty<br />
  {  get { //do something and return integer }<br />
  {  set { //do something and set an integer }<br />
}<br />

I can bind the SelectedIndex property of a ComboBox to MyClass.MyProperty

It all worked great until I run into properties that return enumerated types.
I looked at the IL code of my classes and the enumerated types are int32 so when I call the PropertyInfo.SetValue method with the value casted to int32 my program still crashes. The error I get is System.ArgumentException "Object type cannot be converted to target type".

I did try using Whidbey and it works, but I can't wait until it gets released.

Following is my code:
<br />
private void SetPropertyValue(object obj, string propertyName, string val)<br />
{<br />
	Type objectType = obj.GetType();<br />
	PropertyInfo propInfo = objectType.GetProperty(propertyName);<br />
	string dataType = propInfo.PropertyType.FullName;<br />
<br />
	if (dataType == "System.Integer" || dataType == "System.Int32")<br />
	{<br />
		propInfo.SetValue(obj, Convert.ToInt32(val), null);<br />
	}<br />
	else if (dataType == "System.Decimal")<br />
	{<br />
		propInfo.SetValue(obj, Convert.ToDecimal(val), null);<br />
	}<br />
	else if (dataType == "System.String") <br />
	{<br />
		propInfo.SetValue(obj, val, null);<br />
	}<br />
	else<br />
	{<br />
		Int32 val2 = Convert.ToInt32(val);<br />
		propInfo.SetValue(obj, val2, null);   //<--- crashes right here "System.ArgumentException"<br />
											// "Object type can not be converted to target type"<br />
	}<br />
}<br />

AnswerRe: How do we use PropertyInfo when we don't know data type? Pin
Marc Clifton26-Apr-05 14:16
mvaMarc Clifton26-Apr-05 14:16 
GeneralRe: How do we use PropertyInfo when we don't know data type? Pin
rudy.net26-Apr-05 18:00
rudy.net26-Apr-05 18:00 
GeneralRe: How do we use PropertyInfo when we don't know data type? Pin
Marc Clifton27-Apr-05 0:56
mvaMarc Clifton27-Apr-05 0:56 
GeneralInvoking Control Events in Code Pin
Thoughthopper26-Apr-05 12:22
Thoughthopper26-Apr-05 12:22 
GeneralRe: Invoking Control Events in Code Pin
Polis Pilavas26-Apr-05 12:56
Polis Pilavas26-Apr-05 12:56 
GeneralRe: Invoking Control Events in Code Pin
Marc Clifton26-Apr-05 14:22
mvaMarc Clifton26-Apr-05 14:22 
GeneralRe: Invoking Control Events in Code Pin
leppie26-Apr-05 18:41
leppie26-Apr-05 18:41 
GeneralRe: Invoking Control Events in Code Pin
Marc Clifton27-Apr-05 4:15
mvaMarc Clifton27-Apr-05 4:15 
GeneralRe: Invoking Control Events in Code Pin
leppie27-Apr-05 9:00
leppie27-Apr-05 9:00 
GeneralRe: Invoking Control Events in Code Pin
Marc Clifton27-Apr-05 10:12
mvaMarc Clifton27-Apr-05 10:12 
GeneralRe: Invoking Control Events in Code Pin
Thoughthopper29-Apr-05 12:37
Thoughthopper29-Apr-05 12:37 
Generalconnecting SQLServer with Asp.net(C#) Pin
cishi_us26-Apr-05 11:10
cishi_us26-Apr-05 11:10 
GeneralRe: connecting SQLServer with Asp.net(C#) Pin
Heath Stewart26-Apr-05 11:17
protectorHeath Stewart26-Apr-05 11:17 
Questiondisplay row number in datagrid? Pin
sammyh26-Apr-05 8:57
sammyh26-Apr-05 8:57 
AnswerRe: display row number in datagrid? Pin
Marc Clifton26-Apr-05 11:15
mvaMarc Clifton26-Apr-05 11:15 
GeneralRe: display row number in datagrid? Pin
Heath Stewart26-Apr-05 11:20
protectorHeath Stewart26-Apr-05 11:20 
GeneralRe: display row number in datagrid? Pin
sammyh26-Apr-05 18:42
sammyh26-Apr-05 18:42 

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.