Class reference

  • Thread starter Thread starter Erick Sasse
  • Start date Start date
E

Erick Sasse

How can I make a method that takes a class (not an instance) as
parameter?

And I want this parameter to accept only Windows Form classes, ie
classes that inherits System.Windows.Forms.Form.

Thanks!
 
You cannot. Classes by themselves do not do anything. You can only pass
instances of classes.

What is it that you want to do?
 
Peter said:
You cannot. Classes by themselves do not do anything. You can only
pass instances of classes.

What is it that you want to do?

I want to create a method that receives a form class, create a instance
of it, configure some properties and then show the created form.
 
Erick said:
How can I make a method that takes a class (not an instance) as
parameter?

And I want this parameter to accept only Windows Form classes, ie
classes that inherits System.Windows.Forms.Form.

You can't; C# is not Delphi.

What you can do is to take a Type parameter, and pass typeof(MyForm).
You can't get the compiler to only accept the typeof() Form
descendants; you'll have to do runtime type checking like

public void FormFactory(Type FormType)
{
if (! typeof(Form).IsAssignableFrom(FormType))
throw new ArgumentException(
"FormType is not the typeof() a Form descendant");
// yadda yadda
}
 
Yes, this is an interesting Delphi feature.
Maybe in a future C# version...

You can always create your own (meta)class that emulates the functionality
of Delphi's "class of" functionality.

Joanna
 
Erick said:
Yes, this is an interesting Delphi feature.
Maybe in a future C# version...

I haven't used Delphi for a while, so I'm not sure I remember all the
aspects of the "class of" operator, but I am quite sure it should be
easily reproducible with Reflection in .NET.


Oliver Sturm
 
Joanna said:
You can always create your own (meta)class that emulates the
functionality of Delphi's "class of" functionality.

Hi Joanna, glad to see you here. :)

Do you have any code sample?

Thanks!
 
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
 
Back
Top