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 TypeNotDerivedException : ApplicationException
{
private Type derivedType;
private Type baseType;
public TypeNotDerivedException(Type derivedType, Type baseType)
{
this.derivedType = derivedType;
this.baseType = baseType;
}
public override string Message
{
get
{
return String.Format("{0} does not derive from {1}",
derivedType.Name, baseType.Name);
}
}
}
class InterfaceNotImplementedException : ApplicationException
{
private Type implementingType;
private Type interfaceType;
public InterfaceNotImplementedException(Type implementingType, Type
interfaceType)
{
this.implementingType = implementingType;
this.interfaceType = interfaceType;
}
public override string Message
{
get
{
return String.Format("{0} does not implement {1}",
implementingType.Name, interfaceType.Name);
}
}
}
private class ConstructorNotFoundException : ApplicationException
{
public ConstructorNotFoundException() :
base("No constructor found for these arguments") {}
}
private class ClassMethodNotFoundException : ApplicationException
{
public ClassMethodNotFoundException() :
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.IsInterface)
{
Type t = null;
Type[] a = value.GetInterfaces();
foreach (Type i in a)
{
if (i == fClassType)
{
t = value;
break;
}
}
if (t != null)
fClassType = value;
else
throw new InterfaceNotImplementedException(value, fClassType);
}
else
{
//////////////////
if (!(value.Equals(fClassType) || value.IsSubclassOf(fClassType)))
throw new TypeNotDerivedException(value, fClassType);
fClassType = value;
}
}
}
public object Create(object[] args)
{
Type[] argTypes = new Type[args.Length];
for (int i = 0; i < args.Length; i++)
argTypes
= args.GetType();
ConstructorInfo lConstructor =
fClassType.GetConstructor(BindingFlags.Instance |
BindingFlags.NonPublic|
BindingFlags.Public,
null, argTypes, null);
if (lConstructor == null)
throw new ConstructorNotFoundException();
return lConstructor.Invoke(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 = args.GetType();
{
MethodInfo methodInfo;
Type classType = fClassType;
do
{
methodInfo = classType.GetMethod(funcName, BindingFlags.Static |
BindingFlags.NonPublic
| BindingFlags.Public,
null, argTypes, null);
classType = classType.BaseType;
}
while (methodInfo == null &&
classType != typeof(Object) &&
methodInfo.ReturnType != returnType);
if (methodInfo == null)
throw new ClassMethodNotFoundException();
return methodInfo.Invoke(null, args);
}
}
protected void Proc(string procName, object[] args)
{
Type[] lArgTypes = new Type[args.Length];
for (int i = 0; i < args.Length; i++)
lArgTypes = args.GetType();
{
MethodInfo methodinfo;
Type classType = fClassType;
do
{
methodinfo = classType.GetMethod(procName, BindingFlags.Static //|
BindingFlags.NonPublic
| BindingFlags.Public,
null, lArgTypes, null);
classType = classType.BaseType;
}
while (methodinfo == null && classType != typeof(Object));
if (methodinfo == null)
throw new ClassMethodNotFoundException();
methodinfo.Invoke(null, args);
}
}
}
}
////////////////////////
Joanna
--
Joanna Carter (TeamB)
Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker