How I can access instance from MethodInfo - c#

I have a method receiving a function, usually a method. From that function I can access to the method's class and other stuff using MethodInfo.
I want to be able to access the instance used, if any, when passing the function/method.
Example:
MyFunctionReceivingAMethod(new SomeClass().MethodA)
I want to access at MyFunctionReceivingAMethod to SomeClass instance from MethodA's reflection data. Is that possible?
Update:
I know I can pass the reference to the method but I wonder if its possible to get it using the funtion pointer I already have.

The reflection data in the MethodInfo class are associated with the method in general, not with any particular instance. Thus, you can't get the information you're looking for from that class.
Instead, the Delegate.Target property will return the instance associated with the received delegate (which I'm assuming is what you're looking for). According to its description on MSDN, the property returns:
The object on which the current delegate invokes the instance method, if the delegate represents an instance method; null if the delegate represents a static method.

Related

C# - How get the instance where a MethodInfo come from?

I read that MethodInfo only have information only about the method, nothing about the instance that come from, but there is some trick to the get instance to invoke the method as part of that instance?
For example:
MethodInfo someMethodInfo;
var obj = GetMethodInstance(someMethodInfo);
someMethodInfo.Invoke(obj, null);
The MethodInfo is not associated with any Instance, it discovers the attributes of a method and provides access to method metadata.
The MethodInfo class represents a method of a type. You can use a MethodInfo object to obtain information about the method that the object represents and to invoke the method.
MethodInfo is reflected directly from the Type definition, and as such has no relationship to any particular instance of the type, infact to get the Method Info you must generally get the type either directly with typeof() (so without even having an instance) or by first getting the type definition from obj.GetType().
So already the type definition is divorced from the original instance before you even attempt to get the MethodInfo, hence it would be harder again to obtain the original instance, if there was an instance at all to get.
Check if you can workaround using Delegates, otherwise look at the process where you obtain the methodinfo, and at that time pass through this to keep a reference to the object so that you don't have to try looking for it in the first place.

I have a Methods name as a string value, now I want to call the method that returns a DataTable object

I'm retrieving a list of methods from a class
foreach (MethodInfo item in typeof(RptInfo).GetMethods())
{
if (item.ReturnType.Equals(typeof(DataTable)))
{
lstMethods.Items.Add((item.Name));
}
}
Clearly adding the method names to a list control.
I'm having problem, I guess more correctly, not understanding, the proper use of 'Action or 'Func' in order to call the selected method name and getting it's return value into a dataview control.
I did reference:
How to call a Method from String using c#?
but still not certain as to a correct implementation method. Still new to implementing these concepts
First, note that the typeof(RptInfo).GetMethods() will give you both instance and static methods, with any number of parameters. To call any of these methods, you'll need to supply the instance (unless the method is static), and values for the parameters. I suggest you look into overloads of the GetMethods method if you want to get methods only with specific signatures.
Let's say you have an instance of the RptInfo on which you want to run your methods, and you have enough values to provide for their parameters. Then, when you select your item from the list, you should call something like
DataTable result = typeof(RptInfo)
.GetMethod(lstMethods.SelectedItem.ToString())
.Invoke(instance, new object[]{value1,value2})
as DataTable
Again, look into overloads of the GetMethod method in case you might have methods with the same name but different signatures. In the Invoke call, you'll use null instead of the instance if your method is static, and you will have to provide the proper number of properly typed values to satisfy the method's signature.

Pass the signature of a method and invoke it on another object of the same type

I want to pass a method around and store it. Let's say the method takes one int and returns void. I'd wrap it in Action< int >.
Now, is there a way to invoke that method but not on the object it originated from but a different instance of the same class?
It's kind of hard to be more specific than that but please ask if anything is confusing and I'll try again.
Now, is there a way to invoke that method but not on the object it originated from but a different instance of the same class?
EDIT: Okay, ignore my first attempt. I thought you were talking about a different class.
It sounds like you should actually be wrapping the method in an Action<object, int> (or a more strongly typed version).
Alternatively, you could create a new delegate using:
Action<int> newAction = (Action<int>) Delegate.CreateDelegate(typeof(Action<int>),
newTarget,
oldAction.Method);
You can use the Type.GetMethod overload which takes parameter types. This allows you to get the exact method with the specified parametere types.

What does this c# syntax mean?

Hi
I have a little problem with understanding this kind of syntax
public delegate void DelegateType();
BeginInvoke(new DelegateType(functionName));
Could somebody tell me what exectly mean new DelegateType(functionName). Why do I have to use new keyword ??
See the documentation.
A delegate is a type that holds a method.
You're creating a new instance of a delegate type, pointing to an existing method.
C# 2 adds an implicit conversion from a method group to any matching delegate type.
However, since BeginInvoke doesn't take a specific delegate type (such as System.Action), you always need to explicitly create a delegate instance.
The first statement declares a delegate type, the second statement instantiates a new delegate of DelegateType.
From the corresponding MSDN article (read the article for more information!):
Once a delegate type has been
declared, a delegate object must be
created and associated with a
particular method. Like all other
objects, a new delegate object is
created with a new expression. When
creating a delegate, however, the
argument passed to the new expression
is special — it is written like a
method call, but without the arguments
to the method.
public delegate void DelegateType();
This defines the syntax for a delegate. This is a reference to a method, either static, or an instance method.
When you call BeginInvoke, you're passing a delegate as the parameter. The C# compiler will convert from any explicit delegate type to System.Delegate, but since the parameter is defined as taking any delegate (via System.Delegate), you must explicitly define the type.
When you specify:
new DelegateType(functionName)
You're creating a delegate of a specific type (DelegateType), which is then passed to the function.
Often, newer APIs will use a known type, such as System.Action (which has the same syntax as your DelegateType). If a method takes an "Action", you would not need the definition above, and you could do:
CallMethodTakingAction(functionName);
'DelegateType' is only a type of thing, so like any other type, you want to say "here's one instance of this type", you need to use 'new'.

Using "this" - many instances of class, calling private method

I have a server where every connected client is represented by an instance of client class. This class is responsible for data processing. I always thought that when calling private function even when there are more instances of the same class, I dont need to use "this" keyword because its considered as local call, also:
if (data[1]==2 && data [3]==2)
SendAck();
But when there were more players (more client classes), sometimes the message was sent to another player and the same with other methods. All was OK when I added "this" to all methods call. Is it possible that wihout "this" it calls method from some other class?
Thanks!
EDITL: What about using public members like public dictionary in each instance and then not using this keyword /like when calling TryGetValue/?
No, this isn't possible. "this" is almost all of the time optional. A situation where you would need it would be if you have a method (for example your constructor) that takes parameters of the same name than fields of your class and you want to assign the values of these parameters to the fields, eg:
class Test
{
private string strValue;
public Test(string strValue)
{
this.strValue = strValue;
}
}
I thinks there is another problem in your code. Could you please give more details?
There isn't any difference between SendAck(); and this.SendAck();, if the method is a member of your class. You will always call the method from the instance that is calling the method.
Check what the method actually does when called, using the debugger. You will see that it is the same method, with or without the this keyword.
From C# Specification:
7.5.7 This access
…
When this is used in a primary-expression within an
instance method or instance accessor
of a class, it is classified as a
value. The type of the value is the
instance type (§10.3.1) of the class
within which the usage occurs, and
the value is a reference to the object
for which the method or accessor was
invoked.
I suspect that you have problems with multithreaded access to instances of type that represent connected clients.

Categories