A
admin
I am trying to get reflection to work and have been spending today
trying to understand it better. I am stuck and hoping someone could
point me in the right direction (maybe a different way of doing this).
I have several different types (classes) and all have an IsValid()
function. (ie. Coke.IsValid(), Gatorade.IsValid(), etc) I am using
the below code to dynamically have the right class's IsValid() called
during my app.
// system.reflection
Assembly a = Assembly.GetExecutingAssembly();
Type t = a.GetType(typeNAME); <-- typeNAME is string with
namespace.class pulled from a DB
object obj = Activator.CreateInstance(t);
obj.IsValid(); <-- error here
I get an error that "'object' does not contain a definition for
'IsValid'" ... which I understand, but how does one call the function
when using reflection? I may need to cast it, but that would not make
this dynamic then. (unless you can dynamically cast something?)
Is there a better way to design/pattern a solution? I am interested
in learning how others design a sort of plugin model (if that's the
right wording)
Thanks for any help/ideas/tip you can share.
trying to understand it better. I am stuck and hoping someone could
point me in the right direction (maybe a different way of doing this).
I have several different types (classes) and all have an IsValid()
function. (ie. Coke.IsValid(), Gatorade.IsValid(), etc) I am using
the below code to dynamically have the right class's IsValid() called
during my app.
// system.reflection
Assembly a = Assembly.GetExecutingAssembly();
Type t = a.GetType(typeNAME); <-- typeNAME is string with
namespace.class pulled from a DB
object obj = Activator.CreateInstance(t);
obj.IsValid(); <-- error here
I get an error that "'object' does not contain a definition for
'IsValid'" ... which I understand, but how does one call the function
when using reflection? I may need to cast it, but that would not make
this dynamic then. (unless you can dynamically cast something?)
Is there a better way to design/pattern a solution? I am interested
in learning how others design a sort of plugin model (if that's the
right wording)
Thanks for any help/ideas/tip you can share.