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" <ma****@nospam. pl> wrote in message
news:07******** *************** *****@phx.gbl.. .
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(param s
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 theobject. This is done using Reflection. If the
method "ApplicationLog in(...)"updated member fields in the containing class you could
interrogate theinstance of that class to get the values of those member
fields.
To do this, you use Reflection to get a descriptor object
of the parameteryou 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.GetT ype();
System.Reflect ion.FieldInfo field =
typeDescriptor. GetField("UserN ame");string userName = (string) field.GetValue( myInstance);
As an aside, I'm not sure why you'd want to interrogate
the values that werepassed in on the method call via reflection. If you want
to save them, youneed to persist them in a database or a business entity
object (i.e. abusiness data storage object) for further use.
Hope this helps,
Luke Venediger.