|
That still doesn't explain what you are trying to do with your example above.
- Nick Parker My Blog | My Articles
|
|
|
|
|
|
Yes, thank you, that is what I meant. Sorry I should have been clearer.
"Where do we go to get our good name back?...we go where we always go when a dramatic change is needed. We go to the ballot box" - Al Gore 5/26/04
|
|
|
|
|
|
Thanks, that's a great article. I have got as far as using PInvoke with the OpenProcess and ReadProcessMemory functions, but the article doesn't use the WriteProcessMemory which I'm having trouble with. I got it working and got it to change the area of memory which I am sure is the right place, but it hung the game I'm not sure if I'm just messing with the wrong place or if I'm doing something wrong. Oh well. I was only doing it for a bit of fun.
"Where do we go to get our good name back?...we go where we always go when a dramatic change is needed. We go to the ballot box" - Al Gore 5/26/04
|
|
|
|
|
I work for a printing company and am writing a program (in C#) which displays an image of what is to be printed on our press. However i need the program to also print the image on a normal inkjet printer as well. This image needs to be printed at a resolution of 240 dpi.
Does anyone know if it is possible to set the printers resolution at design time?
|
|
|
|
|
I have a class library for which I am trying to discover it's methods.
The first class works fine since it has all methods marked public. The second method does not because it has all of its methods marked protected.
Here are the two BindingFlags attempts I've tried:
MethodInfo[] items = assembly.GetMethods
(BindingFlags.Public|BindingFlags.Instance|BindingFlags.NonPublic|BindingFlags.Static);
Is there a BindingFlags enum I can use to expose those protected methods or do I have to do something a bit more complex?
Thanks
Michael
This signature left intentionally blank
|
|
|
|
|
Try the following, it works for me:
BindingFlags bf = BindingFlags.Instance | BindingFlags.Public |
BindingFlags.NonPublic | BindingFlags.FlattenHierarchy;
- Nick Parker My Blog | My Articles
|
|
|
|
|
Well, I tried this and it did not work either.
I ran reflector, and it is able to expand the protected methods. So I know there has got to be a way. But flattening the heirarchy did not do it.
I have determined that ReflectionPermissions must be used. Now all I have to do is figure out WHERE!
This signature left intentionally blank
|
|
|
|
|
theRealCondor wrote:
Well, I tried this and it did not work either.
Yes, it does, I have a running example. You need to determine what the accessor is, and since you MethodInfo object inherits from MethodBase you can pass each instance to a function that would perform some check like the following (just an example):
private string GetAccessor(MethodBase mb)
{
string a = string.Empty;
if(mb.IsPrivate)
a = a.Length > 0 ? a += " private" : a = "private";
if(mb.IsPublic)
a = a.Length > 0 ? a += " public" : a = "public";
if(mb.IsAbstract)
a = a.Length > 0 ? a += " abstract" : a = "abstract";
if(mb.IsVirtual)
a = a.Length > 0 ? a += " virtual" : a = "static";
if(mb.IsStatic)
a = a.Length > 0 ? a += " static" : a = "static";
if(mb.IsFamily)
a = a.Length > 0 ? a += " protected" : a = "protected";
return a;
}
- Nick Parker My Blog | My Articles
|
|
|
|
|
Nick,
I understand what you are saying.....but I need to get the instances to be able to process them. In this class I have about 50 methods in the namespace and every combination of BindingFlags has returned only 2 methods. Neither of which are the protected methods I want. Also, as I said previously, the docs indicate that it is based on the reflection permissions for the module.
Now maybe you have something set up where you have a higher permission set on your modules(???). But when I do this...
bf = BindingFlags.Public|BindingFlags.Instance|BindingFlags.DeclaredOnly|BindingFlags.NonPublic|BindingFlags.Static
container = Assembly.LoadFile(objectName);
widgetType = container.GetType("myNamespace.Widgets.LayoutWidget");
items = widgetType.GetMethods(bf);
...I am always getting back exactly 2 methods instead of the 50+ protected methods. When I dug into the MSDN docs, it stated that this call will only return public methods unless you have the proper reflection permission. So I dug into that documentation which did not help much at all. I got it to where I am setting my permission to
ReflectionPermissionFlag.MemberAccess and I do a permission.Assert(). But this has not helped me get anything but the two public methods as well.
This signature left intentionally blank
|
|
|
|
|
Since you're OR'ing BindingFlags.DeclaredOnly , inheritted methods will not be included. The other 48+ methods that aren't being listed are not inheritted, are they?
Also, iff you tried to do something that CAS (code access security) wouldn't let you do, it doesn't gracefully fail - it throws an exception. If your code is run locally, then by default you have FullTrust permissions.
I recommend using the permview.exe utility to help see what permission are required, and examine your .NET CAS configuration (either using mscorcfg.msc for the Framework version under which your code is executed, or using the appropriate caspol.exe utility verison). Asserting those permissions won't help (and is actually not what you want to do - read on...) - you need to have them in the first place.
Asserting grants callers of your code a permission that you already have, even if those callers aren't granted that permission. It can be very dangerous. Demand is what you want to do. Asserting the permission in your own code only benefits callers. For example, if the .NET BCL (base class libraries) must check the registry for a settings regardless of what permission you have, they will assert that permission so you can call that method.
CAS can be a little confusing. There's an article here on CodeProject - Understanding .NET Code Access Security[^] - that may be very helpful and more consise than digging through the .NET Framework SDK documentation.
Software Design Engineer
Developer Division Sustained Engineering, Microsoft
My Articles
|
|
|
|
|
Just for interest sake, as you dont mention,
Does it list private and/or internal members? If so, then we have found a .NET reflection bug. I have seen this when trying to read nested protected attribute classes on fields. Sounds very related. Let me no, plz
Cheers
top secret xacc-ide 0.0.1
|
|
|
|
|
Leppie,
I am seeing public-only methods. In the one class where I am getting the expected methods, the only reason I am getting them is because everything is public. This is in the 1.1 framework.
Is this sounding like something I should bring Microsoft into?
Michael
This signature left intentionally blank
|
|
|
|
|
Is it possible for you to give more details about the class iow base types (maybe listing), how/where it is being accessed? Did you fiddle with .NET security config?
top secret xacc-ide 0.0.1
|
|
|
|
|
The class library is part of our proprietary web engine. My tool sits on top of it managing the externalized data that creates the web pages.
The dll is loaded locally -- I'm not doing anything kinky like over a remote connection. I listed in this thread how I'm doing my call to get the methods of the particular type.
I did not fiddle with the .NET security config since all of the documentation points to just what I have to execute in my code. I am fiddling with ReflectionPermission to Assert() the correct security. However I am also doing this on a tightly managed enterprise developer machine. However I do not think that this is a problem since (as stated earlier) Reflector displays the methods without problem. So I think it is just getting the ReflectionPermission set correctly. Here is the source that is trying to look at the DLL (fat client code) It loads each method as a node in a tree that has the parent nodes already created.
public PageLayoutEditor()
{
InitializeComponent();
commands.AllowDrop = true;
ReflectionPermission sec = new ReflectionPermission(ReflectionPermissionFlag.MemberAccess);
sec.Assert();
}
#region Windows Form Designer generated code
private void PageLayoutEditor_Load(object sender, System.EventArgs e)
{
try
{
string objectName = Path.Combine(@"C:\Inetpub\wwwroot\TCASampleApp\bin", "TCA.UI.dll");
Assembly container = Assembly.LoadFile(objectName);
Type widgetType = container.GetType("Progressive.TCA.Widgets.LayoutWidget");
Type layoutType = container.GetType("Progressive.TCA.LayoutManagers.TCACoreLayoutManager");
SetNames(objectView.Nodes[0], layoutType, "ProcessInstruction");
SetNames(objectView.Nodes[1], widgetType, "ProcessInstruction_");
}
catch (Exception se)
{
MessageBox.Show(this, "Problems were encountered getting the process instructions within the layout manager."
+ System.Environment.NewLine + se.Message);
}
finally
{
SetButtons(true);
actionsBar.Buttons[6].Enabled = ValidatedClipboard(Clipboard.GetDataObject());
}
}
private void SetNames(TreeNode parent, Type assembly, string LookFor)
{
MethodInfo[] items = assembly.GetMethods
(BindingFlags.Public|BindingFlags.Instance|BindingFlags.DeclaredOnly|BindingFlags.NonPublic|BindingFlags.Static);
SortedList names = new SortedList(items.Length);
foreach (MethodInfo item in items)
{
if (item.Name.StartsWith(LookFor))
{
string newItem = item.Name.Substring(LookFor.Length);
names.Add(newItem, newItem);
}
}
for (int i=0; i<names.Count; i++)
{
TreeNode newChild = new TreeNode((string)names.GetKey(i), 0, 0);
parent.Nodes.Add(newChild);
}
}
This signature left intentionally blank
|
|
|
|
|
|
I was trying different things. I guess after awhile I stopped paying attention to the BindingFlags !! Once I pulled that flag everything worked great.
Thanks lippie!
Michael
|
|
|
|
|
Hi all
I'm working with the FileInfo object.
Is there a way to get hold of the detailed attributes of a jpeg using this object?
Like if you right click on the jpeg in windows explorer - properties - summary: title, subject, author etc etc.
If not, are there any other ways?
Any ideas gratefully received
Steven
|
|
|
|
|
|
|
Cheers guys
The description property using the FileVersionInfo was returning an empty string for my jpgs, but the PropertyItems collection does the job great.
Thanks very much!
|
|
|
|
|
I'd like to store components in a TreeStructure and wanted to be able to design the TreeStructure within the designer. So I've made a Class Item which itself has a Collection ItemCollection (this is the same like TreeNode which has itself a TreeNodeCollection).
This should work and it worked, but:
As I add my items and the subitems and want to save with "OK" there is the following error:
"Object not set to an instance of an Object" - this happens only, if I add Items to a level higher than two:
Item1
SubItem (OK)
SubItem (Error when saving)
Item2
Item3
...
I do not know which Object is null.
Can anyone help me with this problem?
Thank a lot!
Stefan
|
|
|
|
|
I know my answer is not what you expected, but if you have a IDE you can go step by step into the code and find out why it doesn`t work. Your problem may come form a bad code line, or a wrong condition.
I hope you understand...
|
|
|
|
|
Thank you for your answer.
I design the Collection in the Form-Designer, so Debugging is not possible.
The problem is that I cannot see what the Designer wants to code because the designer tries to put code and cannot because the code would be wrong, so I can't see what the designer want to write.
Thanks
Stefan
|
|
|
|