"Erick Sasse" <es***********@ nospam.yahoo.co m.br> a écrit dans le message de
news:
ef************* *@TK2MSFTNGP14. phx.gbl...
Hi Joanna, glad to see you here. :)
Fortunately, my forté is OO not just Delphi, so C# is an ideal language and
I find that VS is just bearable; I'd rather use the Delphi IDE though :-)
Do you have any code sample?
I wouldn't suggest you keep the "Class" classname, but here is a quick
version :
////////////////////////////////
class Class
{
class TypeNotDerivedE xception : ApplicationExce ption
{
private Type derivedType;
private Type baseType;
public TypeNotDerivedE xception(Type derivedType, Type baseType)
{
this.derivedTyp e = derivedType;
this.baseType = baseType;
}
public override string Message
{
get
{
return String.Format(" {0} does not derive from {1}",
derivedType.Nam e, baseType.Name);
}
}
}
class InterfaceNotImp lementedExcepti on : ApplicationExce ption
{
private Type implementingTyp e;
private Type interfaceType;
public InterfaceNotImp lementedExcepti on(Type implementingTyp e, Type
interfaceType)
{
this.implementi ngType = implementingTyp e;
this.interfaceT ype = interfaceType;
}
public override string Message
{
get
{
return String.Format(" {0} does not implement {1}",
implementingTyp e.Name, interfaceType.N ame);
}
}
}
private class ConstructorNotF oundException : ApplicationExce ption
{
public ConstructorNotF oundException() :
base("No constructor found for these arguments") {}
}
private class ClassMethodNotF oundException : ApplicationExce ption
{
public ClassMethodNotF oundException() :
base("No class method found for these arguments") {}
}
private Type fClassType;
public Class(Type aType)
{
fClassType = aType;
}
public Type ClassType
{
get
{
return fClassType;
}
set
{
/////////////////////
if (fClassType.IsI nterface)
{
Type t = null;
Type[] a = value.GetInterf aces();
foreach (Type i in a)
{
if (i == fClassType)
{
t = value;
break;
}
}
if (t != null)
fClassType = value;
else
throw new InterfaceNotImp lementedExcepti on(value, fClassType);
}
else
{
//////////////////
if (!(value.Equals (fClassType) || value.IsSubclas sOf(fClassType) ))
throw new TypeNotDerivedE xception(value, fClassType);
fClassType = value;
}
}
}
public object Create(object[] args)
{
Type[] argTypes = new Type[args.Length];
for (int i = 0; i < args.Length; i++)
argTypes[i] = args[i].GetType();
ConstructorInfo lConstructor =
fClassType.GetC onstructor(Bind ingFlags.Instan ce |
BindingFlags.No nPublic|
BindingFlags.Pu blic,
null, argTypes, null);
if (lConstructor == null)
throw new ConstructorNotF oundException() ;
return lConstructor.In voke(args);
}
public object Func(string funcName, object[] args, Type returnType)
{
Type[] argTypes = new Type[args.Length];
for (int i = 0; i < args.Length; i++)
argTypes[i] = args[i].GetType();
{
MethodInfo methodInfo;
Type classType = fClassType;
do
{
methodInfo = classType.GetMe thod(funcName, BindingFlags.St atic |
BindingFlags.No nPublic
| BindingFlags.Pu blic,
null, argTypes, null);
classType = classType.BaseT ype;
}
while (methodInfo == null &&
classType != typeof(Object) &&
methodInfo.Retu rnType != returnType);
if (methodInfo == null)
throw new ClassMethodNotF oundException() ;
return methodInfo.Invo ke(null, args);
}
}
protected void Proc(string procName, object[] args)
{
Type[] lArgTypes = new Type[args.Length];
for (int i = 0; i < args.Length; i++)
lArgTypes[i] = args[i].GetType();
{
MethodInfo methodinfo;
Type classType = fClassType;
do
{
methodinfo = classType.GetMe thod(procName, BindingFlags.St atic //|
BindingFlags.No nPublic
| BindingFlags.Pu blic,
null, lArgTypes, null);
classType = classType.BaseT ype;
}
while (methodinfo == null && classType != typeof(Object)) ;
if (methodinfo == null)
throw new ClassMethodNotF oundException() ;
methodinfo.Invo ke(null, args);
}
}
}
}
////////////////////////
Joanna
--
Joanna Carter (TeamB)
Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker