By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,537 Members | 1,743 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,537 IT Pros & Developers. It's quick & easy.

problems typecast,find by function/member name and getting "managed reference"

P: n/a
HI!
I wrting some program part of it is XML config parser which contains
some commands(for flexibility of engenie).
how do i more simple(if it possible not via System.Reflection or
System.CodeDom.CodeCastExpression)
__problem typecast #1
Desc:i do needed checks but data/commands in XML is dynamic and i don't
wanna fix C# code again and again...
Sample:foreach (object some in somearray)
(some.GetType())some.someaction();

__problem #2 function/member exist and "managed reference"
Desc: how i said there is some name of function/member in XML data
which refers to some function/member in my public C# code i need to
find it and get reference to operate.
Sample:
foreach (String name in memberarray)
if (Core.Public.isMember[name]==True)
foreach (String action in memberarray[name].actionsarray)
if (Core.Public.Member[name].isMember[action]==True)
Core.Public.Member[name].action[action.param];

Sep 22 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
SunnyDrake <su********@ua.fmwrote:
I wrting some program part of it is XML config parser which contains
some commands(for flexibility of engenie).
how do i more simple(if it possible not via System.Reflection or
System.CodeDom.CodeCastExpression)
__problem typecast #1
Desc:i do needed checks but data/commands in XML is dynamic and i don't
wanna fix C# code again and again...
Sample:foreach (object some in somearray)
(some.GetType())some.someaction();
The normal way to overcome this is to use an interface if possible.
Otherwise, you'll need to use reflection to get the appropriate
MethodInfo and invoke it.
__problem #2 function/member exist and "managed reference"
Desc: how i said there is some name of function/member in XML data
which refers to some function/member in my public C# code i need to
find it and get reference to operate.
Sample:
foreach (String name in memberarray)
if (Core.Public.isMember[name]==True)
foreach (String action in memberarray[name].actionsarray)
if (Core.Public.Member[name].isMember[action]==True)
Core.Public.Member[name].action[action.param];
I *think* you're basically asking for reflection here, but I'm not
sure. Here's a sample program if you are:

using System;
using System.Reflection;

class Test
{
static void Main()
{
Test t = new Test();
CallMethod (typeof(Test), t, "FirstMethod");
CallMethod (typeof(Test), t, "SecondMethod");
}

static void CallMethod (Type type, object target,
string methodName)
{
MethodInfo method = type.GetMethod(methodName);
// The "null" here is because we're assuming
// the method has no parameters
method.Invoke (target, null);
}

public void FirstMethod()
{
Console.WriteLine ("I'm in FirstMethod");
}

public void SecondMethod()
{
Console.WriteLine ("I'm in SecondMethod");
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 23 '06 #2

P: n/a
Jon написав:
The normal way to overcome this is to use an interface if possible.
and then use Runtime.InteropServices._Type.GetInterface Method (String)
?
it's Net 2.0 :( and i need to add interface for each class and then add
functions to get variables :(. And interface will be a weak side of
design.
Otherwise, you'll need to use reflection to get the appropriate
MethodInfo and invoke it.
I *think* you're basically asking for reflection here, but I'm not
sure. Here's a sample program if you are:
static void CallMethod (Type type, object target,
string methodName)
{
MethodInfo method = type.GetMethod(methodName);
// The "null" here is because we're assuming
// the method has no parameters
method.Invoke (target, null);
}
thanks it's was a great help!.
now i only need find a way to get object by its name in string..
and still i'm not found a good solution to dynamicaly
typecast(unboxing) any *unknown type* to object at runtime.

Sep 26 '06 #3

P: n/a
SunnyDrake <su********@ua.fmwrote:
Jon ???????:
The normal way to overcome this is to use an interface if possible.
and then use Runtime.InteropServices._Type.GetInterface Method (String)
?
No, and then call methods in the interface.
it's Net 2.0 :( and i need to add interface for each class and then add
functions to get variables :(. And interface will be a weak side of
design.
I guess it depends on the exact situation - but I would *generally*
rather see a "strongly called" interface member than futz around with
reflection. It does depend though.
Otherwise, you'll need to use reflection to get the appropriate
MethodInfo and invoke it.
I *think* you're basically asking for reflection here, but I'm not
sure. Here's a sample program if you are:
static void CallMethod (Type type, object target,
string methodName)
{
MethodInfo method = type.GetMethod(methodName);
// The "null" here is because we're assuming
// the method has no parameters
method.Invoke (target, null);
}
thanks it's was a great help!.
now i only need find a way to get object by its name in string..
Objects don't have names. What exactly do you mean?
and still i'm not found a good solution to dynamicaly
typecast(unboxing) any *unknown type* to object at runtime.
Hang on - casting and unboxing are very different things. Casting at
runtime doesn't make sense because the point of casting is to give the
*compiler* more information, which clearly isn't relevant at runtime.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 26 '06 #4

P: n/a
Jon написав:
The normal way to overcome this is to use an interface if possible.
and then use Runtime.InteropServices._Type.GetInterface Method (String)
No, and then call methods in the interface.
I guess it depends on the exact situation - but I would *generally*
rather see a "strongly called" interface member than futz around with
reflection. It does depend though.
let me explain .. this *program and it's parts* may be used be used be
different peope:
coders which work's on some modules(not core),artists who add new gfx
and add info about it in XML resource list file and designers who via
tool or directly in XML will place objects/ai rules(logic)/actions
etc... so one of core script side task is to load XML try to
find requested command(which name/type/state is unknown , only string
name(or hash number(functions may be created/changed by modules in
runtime or even via DCOM)) find appropriate type/object,initalize it in
C# type and perform actions on it described in XML(partialy solved thnx
to you).So as i thnk reflection and dynamic types is a good way, but as
a former C++ coder i'v hardly hitted in typecast of C#, in this way C#
to bee more *plastic* on base synatx have a long way to go....well you
can call me *stuped C# green*...but i wanna make long look on C# in my
coding.
thanks it's was a great help!.
now i only need find a way to get object by its name in string..
Objects don't have names. What exactly do you mean?
Object's don't have but (pre)boxed data have type and possibly can
still have intalization variable name, other part of it after
initalization putted in some ArrayList as objects so (in my cases it's
classes) they have inside variable name so i need
to get collection of initalized objects of *XML suppiled type name*
and/or get collection of initalized objects which in pre-boxed type
have public string name; of *XML suppiled value* in some domain
assembly.
and still i'm not found a good solution to dynamicaly
typecast(unboxing) any *unknown type* to object at runtime.
Hang on - casting and unboxing are very different things. Casting at
runtime doesn't make sense because the point of casting is to give the
*compiler* more information, which clearly isn't relevant at runtime.
?give struct of data,rule to access data parts in memory at runtime at
least this was so in C++!
this as i think was one of ideas to keep original GetType for objects
and for conversions
too... this is not relevant if code is unmodified(in compiled form)at
runtime,and make sense if same data at runtime maybe used in different
means(types,forms,purpose) without modification or creating new data.

Sep 26 '06 #5

P: n/a
Jon написав:
The normal way to overcome this is to use an interface if possible.
and then use Runtime.InteropServices._Type.GetInterface Method (String)
No, and then call methods in the interface.
I guess it depends on the exact situation - but I would *generally*
rather see a "strongly called" interface member than futz around with
reflection. It does depend though.
let me explain .. this *program and it's parts* may be used by
different peope:
coders which work's on some modules(not core),artists who add new gfx
and add info about it in XML resource list file and designers who via
tool or directly in XML will place objects/ai rules(logic)/actions
etc... so one of core script side task:
1) is to load XML
2) try to find requested command(which type/state is unknown , only
name in string(or hash number,btw. functions may be created/changed by
modules at
runtime or even via DCOM))
3) find appropriate type/object,initalize it in
C# type
4) perform actions on it,which described in XML(partialy solved thnx
to you).
So as i thnk reflection and dynamic types is a good way, but as
a former C++ coder i'v hardly hitted in typecast of C#, in this way C#
to be more *plastic* on base synatx have a long way to go....well you
can call me *stuped C# green*...but i wanna make long look on C# in my
coding(i'm buyed by some ideas and toolz :).
thanks it's was a great help!.
now i only need find a way to get object by its name in string..
Objects don't have names. What exactly do you mean?
Object's don't have,some of (pre)boxed data have type and possibly can
still have intalization variable name, other part of objects directly
after
initalization(in some type) putted in some ArrayList as objects.
Objects in my cases in original type is user classes, mostly have
inside (public string name;)
so i need to get collection of initalized objects of .GetType( *XML
suppiled type name*
) and/or get collection of initalized objects which in pre-boxed type
have public string name; == *XML suppiled value* in some domain
assembly.
and still i'm not found a good solution to dynamicaly
typecast(unboxing) any *unknown type* to object at runtime.
Hang on - casting and unboxing are very different things. Casting at
runtime doesn't make sense because the point of casting is to give the
*compiler* more information, which clearly isn't relevant at runtime.
?give struct of data,rule to access data parts in memory at runtime,at
least this was so in C++!
this as i think was one of ideas to keep original GetType for objects
and for conversions
too...
Casting is not relevant if some data(and code data) is unmodified(in
compiled form)at
runtime(to get new type of data you need to make a copy of current with
new type) ,and make sense if same data at runtime maybe used in
different
means(types,forms,purpose) without modification or creating new data.

Sep 26 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.