|
I have seen information on the web about how to access private methods and properties from outside of a PUBLIC class by using a public method that exposes only the specific property one is interested in. But how does one gain access outside of the class when the entire class is private?
Regards,
Scott
|
|
|
|
|
You don't. Private members are not available to consumers outside the class.
What are you trying to do?
|
|
|
|
|
Sure they are. You can use reflection, expression trees, etc. Thats not to say its recommended, but to flat out say you can't is incorrect.
|
|
|
|
|
Since reflection is excruciatingly slow, it would be nice to know what he's really trying to do with this. If speed is an issue, it's really not going to work very well.
If hyjaking someones work is the reason, well, you where I stand on that...
|
|
|
|
|
SledgeHammer01 wrote: Sure they are.
Cool, than it'd be very easy for you to write a code example. He's talking about a private class, which is not the thing below as you'd might expect;
namespace MyTest
{
private class Test
{
}
}
Try to compile that, and you'll get an error;
Compiler says: Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal.
This would be a private class;
static class Owner
{
private class SomeClass
{
private string SomeMethod()
{
return "Hello world";
}
}
}
Standard reflection would look like the two lines below, which would both return NULL , not the Type "SomeClass";
Type someClassType = Type.GetType("SomeClass");
Type someClassType = Type.GetType("Owner.SomeClass");
Bastard Programmer from Hell
|
|
|
|
|
Cool, how do you think .NET Reflector sees private classes? Or ILSpy? You just need the FULLY qualified name of the class including assembly information.
|
|
|
|
|
SledgeHammer01 wrote: Cool, how do you think .NET Reflector sees private classes? Or ILSpy?
Easy to find out by using ILSpy on Reflector
I know that they're accessible, just wanted to point out that it might be somewhat harder than it initially looks.
SledgeHammer01 wrote: You just need the FULLY qualified name of the class including assembly information.
Type someClassType = Type.GetType("ConsoleApplication7.Owner.SomeClass, ConsoleApplication7, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
someClassType remains null .
Bastard Programmer from Hell
|
|
|
|
|
using System;
using System.Diagnostics;
using System.Reflection;
namespace ConsoleApplication7
{
static class Owner
{
private class SomeClass
{
private string SomeMethod()
{ return "Hello world"; }
}
}
class Program
{
static int Main(string[] args)
{
Type ownerClassType = Type.GetType(
"ConsoleApplication7.Owner, ConsoleApplication7, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
Type someClassType = ownerClassType.GetNestedType(
"SomeClass",
BindingFlags.Instance | BindingFlags.NonPublic);
Object someObject = Activator.CreateInstance(someClassType);
MethodInfo mi = someClassType.GetMethod(
"SomeMethod",
BindingFlags.Instance | BindingFlags.NonPublic);
Object result = mi.Invoke(someObject, null);
Console.WriteLine(result);
Console.ReadKey();
return 0;
}
}
}
Yup, works
Bastard Programmer from Hell
|
|
|
|
|
Eddy Vluggen wrote:
namespace MyTest
{
private class Test
{
}
}
Try to compile that, and you'll get an error;
Hi Eddie,
Yes, but, you can have 'private classes' inside another class (nested) which is not private:
public class ContainsPrivateClassExample
{
private PrivateClass pcInstance;
public ContainsPrivateClassExample()
{
pcInstance = new PrivateClass();
}
private class PrivateClass
{
public int y = 200;
}
} Interesting that both these examples compile, and instances of them behave as if they are marked 'public:'
namespace MyTest
{
class Test
{
}
}
namespace MyTest
{
internal class Test
{
}
} For myself, I follow a discipline of marking all classes which are not nested as 'public.'
The issue of whether or not you should use a private, nested, class: well, I don't see any "one ring to bind them all" kind of rule there; I think that depends on program design, algorithm, context, etc.
best, Bill
"Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye." Miss Piggy"
|
|
|
|
|
Dave -
I am reviewing someone else's code as part of my endeavor to learn more about C# and wondering why someone would type a class as private when it appears to be (in my opinion) not very useful when the methods and properties can't be accessed from outside of it.
Regards,
Scott
|
|
|
|
|
namelkcip wrote: why someone would type a class as private If you have a class marked as 'private,' and the code compiles, then you can be sure the class marked as 'private' is nested within another class.
Please see my detailed response to Eddy Vluggen above[^] for an example.
best, Bill
"Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye." Miss Piggy"
|
|
|
|
|
They're private because they are used internally by the class. This is very common.
Not all methods and properties in a class are meant to be used by outside callers. Calling this stuff without knowing what it's for can cause the class to break completely or behave funny.
|
|
|
|
|
A private nested class is often useful within its enclosing parent class. I do this quite often to either store class-private data (as return types from internal methods, or storage elements in a hash map), or to implement interface objects that need to be returned outside as an interface (e.g. IEnumerable/IEnumerable<OuterClass>).
|
|
|
|
|
Bob -
I am looking at some code where a user wrote a class to get the device ID of a Windows Mobile computer, but put the method inside of a class that was private. There are other methods in another class that get the operating system version, and they are in a public class. The user then wrote a WinForm that updates the text of a label upon form_load to display the detected OS type. So I was really curious as to why the user might have chosen to make the device ID method live inside a private class? By doing so, I wouldn't be able to update a text label on the form with that information, correct? I have read about using a private method with a private set variable and a public get, but I don't know how I would make use of that device ID method when it lives in a class that is private.
Regards,
Scott
|
|
|
|
|
With Reflection. But it's not generally a good idea.
|
|
|
|
|
I'd like to use the asynchronous model with the Socket class to receive data.
The BeginReceive function must be told how many bytes you wish to receive:
public IAsyncResult BeginReceive(
byte[] buffer,
int offset,
int size,
SocketFlags socketFlags,
AsyncCallback callback,
Object state
)
Does this mean that if I pass size = 200 , and there are 199 bytes waiting for me, it won't complete the receive?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
If you read the sample supplied with the document of Socket.BeginReceive Method[^] you will notice that the size specified is the size of the buffer passed to the function. So this size has no relation with the amount of bytes waiting to be received.
In the callback function you determine if all the data has been received or whether there is any more data. If there is more data you call BeginReceive again.
So to answer your question directly when 199 bytes are waiting and you pass a buffer of 200 bytes the receive will complete.
If you had used a buffer of 100 bytes you would have needed to perform two receives to get all the bytes. This means that you would have to put the two receives together in a larger buffer.
I hope I clarified it for you.
0100000101101110011001000111001011101001
|
|
|
|
|
Thank you, that clears it up.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Hi All, I am trying to get in my gridView there are two template fields. I need to get those Rows[0] and Cell[0] value.
Please help.
"if you don't know ask"
|
|
|
|
|
object jashimusValue = dataGridView[0 , 0].Value; Didn't test it on the compiler though.
Bastard Programmer from Hell
|
|
|
|
|
Hello,
I have created small program with one button "OK" and
"DataGridVew1" with one column "Messages",
for storing text messages.
When I start the program , i enter some messages to "DataGridView1"
and when I click "OK Button" ,MessageBox shows with text of textmessage in first row.
For unswering to your question look this sample code:
void Button1Click(object sender, EventArgs e)
{
int row = 0;
int column = 0;
string message;
if (dataGridView1[column,row].Value == null)
{
dataGridView1[column,row].Value = "Empty";
}
message = dataGridView1[column,row].Value.ToString();
MessageBox.Show(message);
}
I think that the answer to your question is obvious.
All the best,
Peric Zeljko
periczeljkosmederevo@yahoo.com
modified 6-Oct-11 14:56pm.
|
|
|
|
|
Hi
How can you set/read file attributes, those under "Summary" tab when you right click a file in File Explorer? (i.e. NOT System.IO.FileAttributes[^])
I need to set things like Subject, Author, Category, Keywords, Comments
Thanks
dev
|
|
|
|
|
Are you talking about the ones provided by Office, or are you talking about custom ones? If it's Office, you can use dsofile[^] to read/write document properties. If you are talking about a custom format, you are going to have to do a lot more work - and work with things such as IPropertySetStorage[^].
|
|
|
|
|
I have several supporting classes that are all exactly the same except for one property, which is a List, something like this :-
public class DecimalClass
{
public int x;
public string y;
public List<decimal> Details;
}
public class StringClass
{
public int x;
public string y;
public List<string> Details;
}
Now in my main class I need to have several of these base classes , and so I thought of having one generic class like so :-
public class MyClass<T>
{
public int x;
public string y;
public List<T> Details;
}
and having a List<MyClass<T>> myClasses in my Main class, but this would mean the List would only be able to hold one Type, and I would need it to hold several different Types, the total number and Types of which would remain unknown until the class is created further down the line. If anyone can understand what I am trying to do, and can suggest a solution I would be more than grateful, as at the moment I am having to create a main class for each combination of sub classes that are required.
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|
Hmmmm. Ultimately all classes derive from object , so you could store the data in a List<object>, but this wouldn't give you any advantage over storing as an ArrayList. This means you'd have a lot of boxing/unboxing to take care of, and you'd end up having to use reflection to work out what type to convert it back to.
Alternatively, you could store it in a Dictionary<Type, List<object>> and use that to avoid having to use the reflection part. Just a thought.
|
|
|
|