473,385 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

System.Reflection and the internal access modifier

This code is contained in one source file in VS 2005 project:

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace ReflectionTest
{
internal class x
{
internal x() { }
public int property_1 { get { return 1; } }
public int property_2 { get { return 2; } }
}
class Program
{
static void Main(string[] args)
{
try{
string s= "{0}={1}";
x x0 = new x();
Console.WriteLine(String.Format(s,"property_1",x0. property_1));
Console.WriteLine(String.Format(s,"property_2",x0. property_2));
Console.WriteLine("\nUsing Get Property");
Console.WriteLine(String.Format(s,
"property_1",
x0.GetType().GetProperty("property_1").GetValue(x0 , null)));
Console.WriteLine(String.Format(s,
"property_2",
x0.GetType().GetProperty("property_2").GetValue(x0 , null)));
Console.WriteLine("\nUsing Get Properties");
foreach (PropertyInfo pi in x0.GetType().GetProperties())
Console.WriteLine(String.Format(s, pi.Name, pi.GetValue(x0, null)));
}catch(Exception e){
Console.WriteLine(e.Message + e.StackTrace );
}
Console.ReadKey();
}
}
}

Produces the following output, as expected.
property_1=1
property_2=2

Using Get Property
property_1=1
property_2=2

Using Get Properties
property_1=1
property_2=2

Changing the access modifier to internal on x.property_1 causes an exception:
Object reference not set to an instance of an object.
On the statement:
Console.WriteLine(String.Format(s,
"property_1",
x0.GetType().GetProperty("property_1").GetValue(x0 , null)));

The statements:
Console.WriteLine(String.Format(s,"property_1",x0. property_1));
Console.WriteLine(String.Format(s,"property_2",x0. property_2));
Still work as expected.

What's special about internal on x.propery_1?
Do I need to use one of the other forms of GetProperty?

Jul 11 '07 #1
1 2416
Hello Brian,

You must use
x0.GetType().GetProperty("property_1", BindingFlags.NonPublic | BindingFlags.Instance)
instead of
x0.GetType().GetProperty("property_1")

the internal accessor cause the member to be not visible to outer of the
class calls.

Regards
Martin
This code is contained in one source file in VS 2005 project:

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace ReflectionTest
{
internal class x
{
internal x() { }
public int property_1 { get { return 1; } }
public int property_2 { get { return 2; } }
}
class Program
{
static void Main(string[] args)
{
try{
string s= "{0}={1}";
x x0 = new x();
Console.WriteLine(String.Format(s,"property_1",x0. property_1));
Console.WriteLine(String.Format(s,"property_2",x0. property_2));
Console.WriteLine("\nUsing Get Property");
Console.WriteLine(String.Format(s,
"property_1",
x0.GetType().GetProperty("property_1").GetValue(x0 , null)));
Console.WriteLine(String.Format(s,
"property_2",
x0.GetType().GetProperty("property_2").GetValue(x0 , null)));
Console.WriteLine("\nUsing Get Properties");
foreach (PropertyInfo pi in x0.GetType().GetProperties())
Console.WriteLine(String.Format(s, pi.Name, pi.GetValue(x0,
null)));
}catch(Exception e){
Console.WriteLine(e.Message + e.StackTrace );
}
Console.ReadKey();
}
}
}
Produces the following output, as expected.
property_1=1
property_2=2
Using Get Property
property_1=1
property_2=2
Using Get Properties
property_1=1
property_2=2
Changing the access modifier to internal on x.property_1 causes an
exception:
Object reference not set to an instance of an object.
On the statement:
Console.WriteLine(String.Format(s,
"property_1",
x0.GetType().GetProperty("property_1").GetValue(x0 , null)));
The statements:
Console.WriteLine(String.Format(s,"property_1",x0. property_1));
Console.WriteLine(String.Format(s,"property_2",x0. property_2));
Still work as expected.
What's special about internal on x.propery_1?
Do I need to use one of the other forms of GetProperty?

Jul 12 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Hin | last post by:
from MSDN, decription for protected internal is "Access is limited to the current project OR types derived from the containing class" Is there any way i can have the access limited to the...
2
by: Chien Lau | last post by:
I frequently define internal UserControl-derived classes in my WinForms apps: internal class MyUserControl:UserControl{ ... } I'll often need to embed these controls in a Form, whose class...
2
by: Anders Borum [.NET/C# MCP] | last post by:
Hello! With C# 2.0 coming up, I was wondering what your thoughts are regarding the introduction of access modifiers on set accessors. Personally, I like the first example, because of its...
2
by: Kolozs, Áron | last post by:
Hi everybody, The C# compiler reports a Compiler Error CS0052 in the following situation: I declared a type marked as "internal": namespace MyNamespace { internal class MyInteralClass
2
by: Mountain | last post by:
If a large framework (or library, application, etc.) has optional parts and some users may not want all parts, it would make sense to break the framework into separate physical assemblies. However,...
9
by: JT | last post by:
Here is the overall structure I will be referring to: End-program ProvideWorkFlow.dll Forms and methods that properly manipulate calls to methods in AccessUtils AccessUtils (a web service)...
0
by: Amelyan | last post by:
Why does this happen? How to fix it? Once in a while I get error in ~/ScriptResource.axd?d=... System.Reflection.TargetInvocationException: Exception has been thrown by the target of an...
1
by: Richard Coltrane | last post by:
Hi there, The docs say that default access modifier is internal. My own experience has been that it is internal. And yet almost every website Ive seen on the web tries to tell me its public?? Am...
9
by: dylan.miller | last post by:
I'm having trouble understanding the internal access modifier. There are many classes in my assembly that should not be accessible outside of the assembly. I've used the internal access modifier...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.