Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 132,119 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,989 people online right now. Registration is fast and FREE... Join Now!




Reflection Error

 
Reply to this topicStart new topic

Reflection Error, Change an internal property using Reflection

zakary
post 22 May, 2008 - 09:35 AM
Post #1


D.I.C Regular

Group Icon
Joined: 15 Feb, 2005
Posts: 401



Thanked 6 times

Dream Kudos: 175
My Contributions


I am trying to change an internal property and I am not sure what is wrong or if I am doing it correctly

this is the code for the internal property it is in a different name space and I cannot change internal to public.
csharp

namespace namespace_one
{
class Class1
{
private bool isDirty = false;

public Class1()
{

}

internal bool IsDirty
{
get { return isDirty; }
set { isDirty = value; }
}
}
}


here is my class and code trying to change it
csharp

namespace namespace_two
{
class Class2
{
private namespace_one.Class1 class1;
public Class2()
{
class1 = new namespace_one.Class1();
SetIsDirty();
}

private void SetIsDirty()
{
System.Type type = class1.GetType();

System.Reflection.FieldInfo feildInfo = type.GetField("IsDirty",
System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance);

(feildInfo.GetValue(class1) as
namespace_one.Class1).IsDirty = true;//Error 'Class1' does not contain a definition for 'IsDirty'
}
}
}


IsDirty is not in intellisense so i know I am doing it wrong.

This post has been edited by zakary: 22 May, 2008 - 11:00 AM
User is offlineProfile CardPM

Go to the top of the page

djkitt
post 22 May, 2008 - 10:31 AM
Post #2


D.I.C Head

**
Joined: 22 May, 2008
Posts: 128



Thanked 13 times
My Contributions


You just need to and an underscore after your boolean variable...

private bool isDirty = false;

should be

private bool isDirty_ = false;
User is offlineProfile CardPM

Go to the top of the page

zakary
post 22 May, 2008 - 11:01 AM
Post #3


D.I.C Regular

Group Icon
Joined: 15 Feb, 2005
Posts: 401



Thanked 6 times

Dream Kudos: 175
My Contributions


that is not what is causing //Error 'Class1' does not contain a definition for 'IsDirty' but you are correct with that error. I had already fix that. Thanks
User is offlineProfile CardPM

Go to the top of the page

zakary
post 22 May, 2008 - 11:52 AM
Post #4


D.I.C Regular

Group Icon
Joined: 15 Feb, 2005
Posts: 401



Thanked 6 times

Dream Kudos: 175
My Contributions


got it this will set the internal property value

csharp


namespace namespace_two
{
class Class2
{
private namespace_one.Class1 class1;
public Class2()
{
class1 = new namespace_one.Class1();
}

private void SetIsDirty()
{
System.Type type = class1.GetType();

System.Reflection.FieldInfo feildInfo = type.GetField("isDirty",
System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance);

fieldInfo.SetValue(class1, true);
}
}
}


This post has been edited by zakary: 22 May, 2008 - 11:53 AM
User is offlineProfile CardPM

Go to the top of the page

djkitt
post 22 May, 2008 - 12:24 PM
Post #5


D.I.C Head

**
Joined: 22 May, 2008
Posts: 128



Thanked 13 times
My Contributions


Hey again,

It looks like you are trying to use FieldInfo on the Property IsDirty. This returns null as IsDirty is not a field.

You can set the field isDirty_ directly.


CODE

            System.Reflection.FieldInfo feildInfo = type.GetField("isDirty_",  
                System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic |  
                System.Reflection.BindingFlags.Instance);



            feildInfo.SetValue(class1,true);


Looks like you already got it. Sorry.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/21/08 10:43AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month