Asp.net mvc 当SomeClass作为类型传递时,如何获取SomeClass的

Asp.net mvc 当SomeClass作为类型传递时,如何获取SomeClass的,asp.net-mvc,generics,model-binding,Asp.net Mvc,Generics,Model Binding,我正在尝试使用模型绑定提供程序进行模型绑定 当点击GetBinder方法时,我想根据传入的内容提供一个模型绑定器 我有一个通用模型IBaseModel,其中T:IEntity 我可以从类型中获取BaseModel,但我真正想要的是BaseModel上的,这是一个属性 提前感谢我知道您想要获得通用参数的类型,您可以尝试以下方法: public static Type GetBinder(Type modelType) { Type baseModel = model

我正在尝试使用模型绑定提供程序进行模型绑定

当点击GetBinder方法时,我想根据传入的内容提供一个模型绑定器

我有一个通用模型IBaseModel,其中T:IEntity

我可以从类型中获取BaseModel,但我真正想要的是BaseModel上的,这是一个属性


提前感谢

我知道您想要获得通用参数的类型,您可以尝试以下方法:

    public static Type GetBinder(Type modelType)
    {
        Type baseModel = modelType.GetInterface(typeof (IBaseModel<>).Name);

        if (baseModel != null)
        {
            if (baseModel.IsGenericType)
            {
                var interfacesTypes = modelType.GetInterfaces();
                var typeGeneric = interfacesTypes.FirstOrDefault(x => x.IsGenericTypeDefinition);
                if (typeGeneric != null)
                {
                    return typeGeneric.GetGenericArguments().First();
                }

            }
        }
        return null;
    }

    public interface IBaseModel<T> where T : IEntity
    {

    }

    public class Musica : IBaseModel<Artista>
    {

    }

    public class Artista : IEntity
    {

    }

克里斯,这正是我所需要的。我不知道该如何表达我的问题,这就是为什么我没有得到答案的原因。太酷了。正如你所说的,知道要搜索什么并不容易。我知道它们被称为通用参数,所以我能够立即到达那里
    public static Type GetBinder(Type modelType)
    {
        Type baseModel = modelType.GetInterface(typeof (IBaseModel<>).Name);

        if (baseModel != null)
        {
            if (baseModel.IsGenericType)
            {
                var interfacesTypes = modelType.GetInterfaces();
                var typeGeneric = interfacesTypes.FirstOrDefault(x => x.IsGenericTypeDefinition);
                if (typeGeneric != null)
                {
                    return typeGeneric.GetGenericArguments().First();
                }

            }
        }
        return null;
    }

    public interface IBaseModel<T> where T : IEntity
    {

    }

    public class Musica : IBaseModel<Artista>
    {

    }

    public class Artista : IEntity
    {

    }