Finding a constructor with a parameter derived from a common baseclass.

J

jehugaleahsa

Hello:

I have a class heirarchy. The classes optionally support ctors that
can take classes in the same heirarchy. This allows for run-time
decoration.

Is there a way, using reflection, to search for a ctor that has a
parameter that is of a type derived from a base class/interface?

Thanks,
Travis
 
P

Peter Ritchie [C# MVP]

You can't directly search for that. You'd have to enumerate the constructors
checking the parameters to see if any particular parameter satisfied
Type.IsSubclassOf. For example:

public static ICollection<ConstructorInfo>
FindConstructorsWithParameterType(Type classType, Type parameterBaseType)
{
List<ConstructorInfo> list = new List<ConstructorInfo>();
foreach (ConstructorInfo constructorInfo in classType.GetConstructors())
{
foreach (ParameterInfo parameterInfo in constructorInfo.GetParameters())
{
if (parameterInfo.ParameterType.IsSubclassOf(parameterBaseType))
{
list.Add(constructorInfo);
}
}
}
return list;
}
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

You need to get to the parameter level, like

result.GetType().InvokeMember( "ToString",
System.Reflection.BindingFlags.InvokeMethod, null, result, new object[] {
p});
object w = ((System.Reflection.MethodBase)o.GetType().
GetMember("memberName",
System.Reflection.MemberTypes.Method,
System.Reflection.BindingFlags.Instance)[0]).GetParameters()[0].GetType().IsSubclassOf(
typeof(object));
 
J

jehugaleahsa

You can't directly search for that.  You'd have to enumerate the constructors
checking the parameters to see if any particular parameter satisfied
Type.IsSubclassOf.  For example:

public static ICollection<ConstructorInfo>
FindConstructorsWithParameterType(Type classType, Type parameterBaseType)
{
        List<ConstructorInfo> list = new List<ConstructorInfo>();
        foreach (ConstructorInfo constructorInfo in classType.GetConstructors())
        {
                foreach (ParameterInfo parameterInfo in constructorInfo.GetParameters())
                {
                        if (parameterInfo.ParameterType.IsSubclassOf(parameterBaseType))
                        {
                                list.Add(constructorInfo);
                        }
                }
        }
        return list;

}

--
Browsehttp://connect.microsoft.com/VisualStudio/feedback/and vote.http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#








- Show quoted text -

Thanks!
 
J

jehugaleahsa

You can't directly search for that.  You'd have to enumerate the constructors
checking the parameters to see if any particular parameter satisfied
Type.IsSubclassOf.  For example:

public static ICollection<ConstructorInfo>
FindConstructorsWithParameterType(Type classType, Type parameterBaseType)
{
        List<ConstructorInfo> list = new List<ConstructorInfo>();
        foreach (ConstructorInfo constructorInfo in classType.GetConstructors())
        {
                foreach (ParameterInfo parameterInfo in constructorInfo.GetParameters())
                {
                        if (parameterInfo.ParameterType.IsSubclassOf(parameterBaseType))
                        {
                                list.Add(constructorInfo);
                        }
                }
        }
        return list;

}

--
Browsehttp://connect.microsoft.com/VisualStudio/feedback/and vote.http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#








- Show quoted text -

How does one limit the upper bound? I don't want to call a constructor
that requires a more derived type than what I can provide.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top