469,645 Members | 1,162 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

Reflection and private methods

Hi could someone explain to me why the followind code fails
using System;
using System.Collections;
using System.Reflection;
using MbUnit.Core.Framework;
using MbUnit.Framework;

namespace CarpetsX.DataLayer
{
[TestFixture]
public class ClassName
{
[Test]
public void MethodName()
{
className1 class1=new className1();
PropertyInfo property =
class1.GetType().GetProperty("Collections",Binding Flags.NonPublic|Bindin
gFlags.Instance);
Assert.IsNotNull(property);
}
}

public class className1:Class1
{
public className1()
{
}
}

public abstract class Class1
{
public Hashtable collections;

public Class1()
{

}
private Hashtable Collections
{
get
{
if (this.collections == null)
{
this.collections = new Hashtable();
}
return this.collections;
}
}

}
}

and how can i make it pass without chaninging the structure of Class1 of
course.

thnks

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #1
2 2577
tolisss,

Your problem here is not the reflection but the fact that your property is
private and not protected. By definition private members will not be visible
to an inherited class, therefore the property is not visible to className1.
To get this example to work you need to make the property protected not
private.

Sorry there is no way to make this work without changing the structure of
Class1. Here are the changes I made to get this to work.

Hope this helps :)

public abstract class Class1
{
public Hashtable collections;

public Class1()
{
}

protected Hashtable Collections
{
get
{
if (this.collections == null)
{
this.collections = new Hashtable();
}
return this.collections;
}
}
}

"tolisss" wrote:
Hi could someone explain to me why the followind code fails
using System;
using System.Collections;
using System.Reflection;
using MbUnit.Core.Framework;
using MbUnit.Framework;

namespace CarpetsX.DataLayer
{
[TestFixture]
public class ClassName
{
[Test]
public void MethodName()
{
className1 class1=new className1();
PropertyInfo property =
class1.GetType().GetProperty("Collections",Binding Flags.NonPublic|Bindin
gFlags.Instance);
Assert.IsNotNull(property);
}
}

public class className1:Class1
{
public className1()
{
}
}

public abstract class Class1
{
public Hashtable collections;

public Class1()
{

}
private Hashtable Collections
{
get
{
if (this.collections == null)
{
this.collections = new Hashtable();
}
return this.collections;
}
}

}
}

and how can i make it pass without chaninging the structure of Class1 of
course.

thnks

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #2
The other way you can get this to work is to go after the "Collections"
property directly through the Class1 class, rather than through the
className1 class, which is a child class of Class1 and so can't see
"Collections," as mavramov pointed out:

PropertyInfo property = typeof(Class1).GetProperty("Collections", ...
);

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Derek Hart | last post: by
8 posts views Thread by Rachel Suddeth | last post: by
3 posts views Thread by Lee Crabtree | last post: by
1 post views Thread by learning | last post: by
6 posts views Thread by Cralis | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.