how to detect parameter values passed to method

  • Thread starter Thread starter Marcin
  • Start date Start date
M

Marcin

Hello!

Is there any method to detect parameters values passed to
called method?

For example:

public Guid ApplicationLogin(string userName, string
password, int dbId)

was called:

ApplicationLogin("test","test",1);

I know that inside method ApplicationLogin I can detect
what method was called
and what parameters it has using object StackFrame:

StackFrame sf = new StackFrame(0);
sf.GetMethod().GetParameters() - list of parameters

But how can I detect those parameters value?
Anybody have any ideas?

Thanks

Marcin
 
Thank you for help - but in my case i don't store
parameter values in called object fields so i can't
retrieve them using reflection.

I want to write generic function which takes parameters
passed to any method and then use reflection method Invoke
to call another method with the same parameters.

I need something like access to parameters declared with
params keyword (f.e. method declaration: UseParams(params
int[] list) ), but that must work with method that use
standard parameters (without params keyword).

I don't even know if it's possible, but maybe somone knows
the answer.

Thanks

Marcin
 
Hi Marcin,

It is possible to have a generic method that takes in an array of parameters
(using params keyword in your method signature) and invoke a method on an
object. You'll need a way to determine which method you want to invoke,
either by passing in a method name or by counting the number of input
parameters you receive and matching them to a method signature in the target
object.

It is only possible to write a generic scalar method if you have a fixed
number of arguments, and a way to determine which method to call based on
some ID or indicator. You will need to pass all arguments in the
lowest-common-denominator type, which in most cases is System.Object.

Either way, the concept of a generic method is not a good one, it will make
your code path unclear and could also lead to maintenance problems in the
future.

Hope that helps,
Luke Venediger.

Marcin said:
Thank you for help - but in my case i don't store
parameter values in called object fields so i can't
retrieve them using reflection.

I want to write generic function which takes parameters
passed to any method and then use reflection method Invoke
to call another method with the same parameters.

I need something like access to parameters declared with
params keyword (f.e. method declaration: UseParams(params
int[] list) ), but that must work with method that use
standard parameters (without params keyword).

I don't even know if it's possible, but maybe somone knows
the answer.

Thanks

Marcin
-----Original Message-----
Hi there,

You can get the value of fields in an object if you have an instance of the
object. This is done using Reflection. If the method "ApplicationLogin(...)"
updated member fields in the containing class you could interrogate the
instance of that class to get the values of those member fields.

To do this, you use Reflection to get a descriptor object of the parameter
you want to look up. In this case, it will be describing
MyCustomClass.UserName. Of course, you start with the System.Type object.

To get your field:

System.Type typeDescriptor = myInstance.GetType();
System.Reflection.FieldInfo field = typeDescriptor.GetField("UserName");
string userName = (string) field.GetValue(myInstance);

As an aside, I'm not sure why you'd want to interrogate the values that were
passed in on the method call via reflection. If you want to save them, you
need to persist them in a database or a business entity object (i.e. a
business data storage object) for further use.

Hope this helps,
Luke Venediger.
 
Back
Top