C# - Invoking Private / Protected Methods Via Reflection From The Same Object Instance (or Base)

Jan 5, 2012

Is it possible to call a protected method via reflection. I am using this:

Me.GetType.InvokeMember(Stages(CurrentStage),
Reflection.BindingFlags.InvokeMethod,
Nothing,
Me,
Nothing)

Which always calls a method with no params or return. The idea is that the user of my object (transactional processing of business logic) inherits the base class adds in a load of private methods to fire. They then add these method names to a list in the order they would like them fired and the code above will take care of firing them.

It works fine with public methods but not with private or protected methods in the same class (Protected because I have some 'standard' pre built methods to add to the base class). Realistically I could make the methods public and be done with it but my inner nerd wont allow me to do so...

I am assuming this is a security feature. Is there a way to get around this or does anyone have any suggestions on how to proceed but keep my tasty, tasty visibility modifiers in tact?

(NOTE: ITS IN VB.NET but a C# Answer is fine if that is what you are comfortable with).

View 3 Replies


ADVERTISEMENT

Reflection - Find From A Property Info Object If That Property Has A Non Public (Private / Protected) Setter?

Aug 27, 2009

I searched on the forum / Internet for the solution how a PropetryInfo object (of a Public property) can reveal if it has a Private Protected Setter ... it was all in vain .... all help I found was about how to "Set" value of a public property having a Private Setter.I would like to know if I have a PropertyInfo object of a public property, how would I know if its Setter is Non Public?

I tried, in a exception handling block, where I did a GetValue of the PropertyInfo object and then called SetValue by setting the same value back... but to my surprise it worked well and didn error out.

[Code]...

View 1 Replies

.net - Reflection Retrieve Private Methods Of A Class?

Sep 13, 2010

I want to retrieve private (implementation and other) methods of a class which implements an interface and also is derived from (inherits) a base class.How can I achieve this using reflection?This is wat m tryin to do. I need to view these private methods and their contents, I don't want to invoke them.

Dim assembly As System.Reflection.Assembly
Dim assemblyName As String assemblyName = System.IO.Path.GetFullPath("xyz.dll")
assembly = System.Reflection.Assembly.LoadFile(assemblyName)
assembly.GetType("myClass").Getmethods(Bindings.NonPublic)
assembly.GetType("myClass").GetMethods(BindingFlags.NonPublic) isn't working

View 2 Replies

.NET Be Forced To Initialize Instance Variables BEFORE Invoking The Base Type Constructor?

Jan 5, 2011

After debugging a particularly tricky issue in VB.NET involving the order in which instance variables are initialized, I discovered that there is a breaking discrepancy between the behavior that I expected from C# and the actual behavior in VB.NET.Nota bene: This question concerns a slight discrepancy in the behaviors of VB.NET and C#. If you're a language bigot that is unable to provide an answer other than "that's why you should use C#, noob", there is nothing for you to see here; kindly move along.Specifically, I expected the behavior outlined by the C# Language Specification (emphasis added):

When an instance constructor has no constructor initializer, or it has a constructor initializer of the form base(...), that constructor implicitly performs the initializations specified by the variable-initializers of the instance fields declared in its class. This corresponds to a sequence of assignments that are executed immediately upon entry to the constructor and before the implicit invocation of the direct base class constructor. The variable initializers are executed in the textual order in which they appear in the class declaration.

Contrast that with the portion of the VB.NET Language Specification concerning Instance Constructors, which says (emphasis added): When a constructor's first statement is of the form MyBase.New(...), the constructor implicitly performs the initializations specified by the variable initializers of the instance variables declared in the type. This corresponds to a sequence of assignments that are executed immediately after invoking the direct base type constructor. Such ordering ensures that all base instance variables are initialized by their variable initializers before any statements that have access to the instance are executed.

The discrepancy here is immediately obvious. C# initializes class-level variables before calling the base constructor. VB.NET does exactly the reverse, apparently preferring to call the base constructor before setting the values of instance fields.If you want to see some code, this related question provides a more concrete example of the divergent behavior. Unfortunately, it does not provide any hints as to how one might coerce VB.NET into following the model established by C#.

I'm less interested in why the designers of the two languages chose such divergent approaches than I am in possible workarounds for the problem. Ultimately, my question is as follows: Is there any way that I can write or structure my code in VB.NET to force instance variables to be initialized before the base type's constructor is called, as is the standard behavior in C#?

View 2 Replies

Invoking A Method From A Class Dynamically With Reflection?

Oct 15, 2010

I'm making a little program in where i'm trying to invoke a method from a class dynamically with so called Reflection.The class I'm trying to call is called ContactList and i try to invoke the method in this class called count. The assembly itself is called Contact.ExeNow I have the following code:

Public Function InvokeContacts() As Integer
Dim ContactsAssembly As Assembly = Assembly.LoadFrom("Contacts.exe")
Dim Mycontactlist As Type = ContactsAssembly.GetType("ContactList")

[code]....

View 10 Replies

Invoking A Method Via Reflection Causes System.MissingMethodException?

Dec 5, 2011

I am loading an assembly dynamically and invoking a static method from it. The problem arises when the method uses a reference which is not trivial (e.g. mscorlib or System.Core) - I get System.MissingMethodException. I have tried going through the references assemblies of the loaded assembly and manually loading them all, thus forcing them to be loaded onto the AppDomain. I have checked CurrentDomain.GetAssemblies, the assemblies are loaded.

View 1 Replies

.net - Invoking COM Properties And Methods

Nov 22, 2010

I am trying to dynamically create COM object, call COM method and set COM properties. The COM class is a VB6 ActiveX DLL. The implementation is exactly equal to the VB6 code from this page

[Code]...

View 1 Replies

Making Class Methods Instead Of Instance Methods In .NET?

Mar 29, 2010

I am not sure how clear my question is by the title, but I am trying to make Class methods instead of Instance methods in Visual Basic that way I don't have to waste memory and code creating temporary objects to execute methods that don't need instance variables.

I am not sure if you can do that in VB but I know you can in Objective-C by using either a "+" or "-" sign in front of the method declaration. And in C++ (at least I think, I can't remember) you put the static keyword or const keyword in front of the function.How would I do this in VB if it is possible? Or should I just make a separate set of functions that are not members of a class?

View 2 Replies

Private Or Protected Set For A MustOverride Property?

Jul 24, 2010

I'd like to have a Private or Protected "Setter" for a property that also happens to be an abstract (MustOverride). I'm porting some code from C# to VB and in C# this is pretty straight forward. In VB not so much (for me anyway).

Some code...

In C#...
public abstract class BaseClassWithAnAbstractProperty
{
public abstract int AnAbstractIntegerProperty { get; protected set; }
}

[Code]....

The issue seems to be the inability to flesh-out the Get/Set specifics in the declaration.

View 2 Replies

Private Vs Protected - Button Click Event Is Created In ASP.NET ?

Feb 22, 2012

In ASP.NET using VB.NET, you can define the wired up button click event (to an ASP.NET server control) in 2 different ways (for the purpose of this conversation - manually wiring up via button property not in question here):

Double click on the button in the designer which produces an event in the code behind with a Protected method.In the code behind, select the button from the list of controls, and then select it's 'Click' event. This produces a Private method.I understand the difference between Private and Protected; why based on how the wired up event is autocreated it generates a different Access Level on the method?

View 1 Replies

What Use Cases Exist For Non-static Private Or Protected Events

Sep 25, 2009

What purpose do protected or private (non-static) events in .NET really serve?It seems like any private or protected event is more easily handled via a virtual method. I can (somewhat) see the need for this in static events, but not for normal events. Have you had a use case before that clearly demonstrates a need or advantage for a non-static protected or private event?

View 5 Replies

VS 2008 - Difference Between Public, Private, Protected When Declaring Functions And Subs?

Jul 19, 2009

i have have a few questions about the syntax of the lan.:

1) What is the 'Get' statement and when do you use it?

2) Whats the difference between public, private, protected etc... when declaring functions and subs.

3) When would you use the overrides property?

View 4 Replies

Access The Events And The Methods Of These Assemblies Directly Without Always Using Reflection?

Mar 6, 2012

If I have an application that load some assembly at runtime. Is it possible to access the events and the methods of these assemblies direcly, without always using Reflection

View 15 Replies

Calling Methods In Non-assembly (non-managed) Dlls Through Reflection In .net?

Dec 26, 2011

I want to use reflection to dynamically call a dll (not an assembly, non-managed, possibly COM object model) in vb.net.I've seen several different methods of calling a method in a dll that is a .net assembly - but I have not found the way to dynamically call a method inside of a dll like user32.dll or winmm.dll. I believe this would be called late-binding?

The reason I'm asking is because I am building a language that will depend on external libraries for lots of different functionality. An example (in this language I'm building):

Declare Function mciSendStringA using "winmm.dll" (strCommand As String, strReturn As String, returnLength As Integer, blah As Integer) As Integer

Declare Function WriteConsoleA using "kernel32.dll" (hConsoleOutput As Integer, lpBuffer As String, numberofcharstowrite as Integer, lpReserved as Integer) As Boolean

When I build the interpreter for the call to mciSendString in winmm.dll, how can I use reflection to handle this? I want the user to be able to reference any dll/method they wish.Can I get some guidance in the right direction? Perhaps even some vb.net code that I can take apart and understand?

View 1 Replies

.net - Can't Reflect On Private Methods?

Jan 5, 2012

So I created this thread: Invoking Private / Protected Methods Via Reflection From The Same Object Instance (or Base) And we got the problem fixed save for private methods. As this may not be the same issue I thought it may be best to post a different question with the full source. It is still a work in progress but it is functional.

[Code]...

So this class is being inherited by a (so far) empty child class and ProcessStage is being called. Notice that ConfirmFormDataIsValid() sub is private. If you run this it will not find this method. If I change it to protected however it works fine.

View 2 Replies

C# - When Should Use Public / Private / Static Methods

Apr 27, 2009

I'm new to C#.Till this moment I used to make every global variable - public static.All my methods are public static so I can access them from other classes. I read on SO that the less public static methods I have,the better.So I rewrote my applications by putting all the code in one class - the form class.Now all my methods are private and there's no static method.

My question: What should I do,keeping everything in the form class is dump in my opinion. When should I use public,when private and when static private/public? I get the public methods as a 'cons' ,because they can be decompiled,but I doubt that.My public methods can be decompiled too.What is so 'private' in a private method? EDIT: I'm not asking how to prevent my program to be decompiled,I'm asking whether I should use static,private and public.And also : Is there are problem in putting all the code in the form class so I dont have to use public methods?

View 6 Replies

Calling Private Shared Methods?

Aug 8, 2011

I have a form (frmMain) that contains a custom control (con class). frmMain contains a combo box control and when the user selects a value from it, I pass this value to a sub routine within con class (see red line below). This purpose of the sub routine is to populate a set of comboxes contained in con class according to the value that is passed in.

Private Sub cboGuidelines_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboGuidelines.SelectedIndexChanged
If bChanged = True Then

[code].....

View 6 Replies

How Variables Declared With Protected Access In A Base Class Are Used To Implement Inheritance

Jul 29, 2010

how variables declared with protected access in a base class are used to implement inheritance.

View 1 Replies

Using Reflection To Attach / Detach Handler Of Base Event In Derived Class

Dec 13, 2009

I am just curious, if there is some way to attach handler to my derived classes base event, if I know EventInfo and I have Delegate to handler function. I have tried MyEvent.AddHandler(CType(Me, BaseClassType), MyDelegate) already, but no positive result at all. Consider being in need of attaching handler to custom event at runtime, while derived class possibly shadows this event.

View 2 Replies

Why Does C# Set Private Variables Before The Base Constructor While .NET Does The Opposite

Mar 3, 2011

comparing C# code and VB.NET and the results between the seemingly identical code were entirely different.(Why C# is always winning over VB.NET?)The explanation given is that C# will initialize the class fields, then call the base constructor, but VB.NET does the exact opposite. Is there a technical reason for the languages to be different? At first glance, it seems that either approach is equally valid, but I can't fathom why they wouldn't have selected the SAME approach.

EDIT: As 'Jeffrey L Whitledge' has pointed out, VB6 did not have inheritance, so I don't think we can say 'to keep VB.NET and VB6 more closely related'.

View 1 Replies

OOP - Shared Methods And Base Properties?

Feb 25, 2009

If you can't use Me. in a non-instance method, how would you use properties/functions from a base class in a Shared method?

For example, ClassB inherits from ClassA
ClassB has a Shared Method DoWork();
ClassA has a ReadOnly Property SecurityKey

How do I Public Sub DoWork() Dim test as String = Me.SecurityKey End Sub

View 5 Replies

Flattening An Object Hierarchy Using A Shim Class - Initializing Derived Object Properties From The Base Object?

Jul 10, 2010

I am somewhat new to object oriented programming and am attempting to flatten a Linq object hierarchy by using a shim class.how to initalize a derived class with property values from a base class?I have two objects, a base object with about 100 properties, and a derived object which inherits from the base object and adds a few additional properties beyond those of the base object. My simple constructor creates the derived object, but I am looking for a way to initialize the derived object properties with values from the base object.Right now I am using reflection to iterate over the properties individually and I suspect there may be a better way. The following example shows my shim class constructor for the derived class, and two properties:

newProperty1 - a new string property of the derived class

flattenedProperty2 - a new string property of the derived class, copied from a 2nd-level object of the base class

Code example:

Public Class derivedObj
Inherits baseObj
Private _newProperty1 As String[code].......

Is this the correct constructor approach to flatten the object hierarchy using a shim class? My second question relates to initialization of properties in the derived class. The constructor above creates the derived object, but what is the best way to initialize the derived object properties with values from the base object? The following code uses reflection to iterate over the properties individually, but I suspect there may be a better way.

Code example:

' property names are in the string array fieldNames

'baseObjQuery is an ienumerable of baseObj

'derivedObjList is a list of derivedObj[code].....

Is there a simple way to initialize values for the properties in the derived object based upon the values of the common properties in the base object?

View 7 Replies

Cant See Available Methods List When Write Object.METHODS?

Jun 24, 2011

I am working with a vb program, but there is something strange on one of my .vb code pagewhen i put the "dot" afther the object name its dont show the methods availables for this objectbut on other vb code pages i can see it. but in this one no.for exmaplethis is a piece of code: Dim sb As New StringBuilder()

View 3 Replies

Private Structure Methods - 'implement' The BringToFront Method

Jan 13, 2012

I have the following

Private Structure FadeLabel
Dim Alpha As Byte
Dim Color As Color

[CODE]...

Which I picked up from this thread: [URL]

What I'd like to do is 'implement' the BringToFront method so that when I use the 'FadeLabel' structure I can also call it's method 'BringToFront'.

View 1 Replies

Reference Property Of Instance Via Reflection

Jan 2, 2010

When I got a fieldsinfo-array of a class I know all of it's fields. Now, how can I access all of the fields of an instance of the same class by using the fieldsinfo-array? [code]I'm sure there exists a possiblity to get the value of the instance, but how?

View 5 Replies

Invoking Onmousedown Action Of Object In WebBrowser Control

Aug 21, 2009

I need to trigger the onmousedown property of a table contained on a web page presented in the WebBrowser control of my form.The site is a nested frames mess, but in this particular frame the tables are basically used to create large, context buttons. In other words, you click the table and are navigated to a subform.[code]At first I thought this might be handled by using InvokeMember("click"), but that has no effect. If I try InvokeMember ("onmousedown") I receive a javascript error on the page. Can anyone tell me if/how one invokes the onmousedown property of an object inside of the WebBrowser control?

View 4 Replies

.net - Invoking Interface Method Using System.Object Type Variable

Jan 6, 2011

I have an interface ITest with a method GetResult(). I have a class Test which implements ITest and thereby defines private method GetResult().

Next I create an instance of Test in a different class. The code is as below:

Module NewClass
Public Sub New()
Dim i As ITest = New Test()

[Code]....

I am migrating existing code from VB 6 to VB.NET and hence I'm not supposed to change the access modifier of GetResult. Leaving it Private will throw InvalidCastException Unable to cast object of type 'System.Object' to type 'ITest'

Object type variable o is used in many places and hence I don't want to change that. And yes, Test implements ITest.GetMember with a different name.

View 3 Replies

Get "Object Reference Not Set To An Instance Of An Object" Error When Trying To Find An Instance In A String?

Jan 23, 2009

I am using the IndexOf function of the string class to find a specific instance of a string. And based on that I have a logic to do something. Below is the example of what I am doing. The code is in production and is working fine. But for some instance I am getting an "Object reference not set error". Yeah its intermittent.The requestXML is a string of xml data passed in as a paramter to a subroutine. I am fetching the xml string as a form post in a request object. The only thing I could think of getting this kind of error is when the requestXML string varia

requestXml = Request("requestXML")
SubmitRequest(requestXml)
Private
Sub SubmitRequest(ByVal requestXml

[code]....

View 5 Replies

VS 2010 Using Reflection Difference Between Static And Instance Fields And Properties

Jun 6, 2012

I'm trying to obtain the values using the prospective GetValue(s) methods but I don't understand the syntax of the documentation. I'm sending the correct bindings (I believe) to filter the fields, and properties that i'm interested in usually

[Code]...

View 3 Replies

Access A "private Virtual" Property Starting With Underscore Through Reflection?

Apr 10, 2011

I was trying to access the following property using Reflection because I don't have the original source code (suppose this was decompiled through Reflector). It seems that something is special about it being "private virtual" or maybe because it has "_" in the beginning of the property. I can access all the other private properties no problem except this one. Just can't figure out what I am doing wrong:

private virtual String _MyProperty
{
get

[code]......

View 5 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved