C# 如何将非泛型方法添加到包装泛型方法
ASP.NET MVC4 razor帮助程序无法将类型参数传递给泛型方法。要解决这个问题,最好的方法可能是添加带有签名的非泛型get方法C# 如何将非泛型方法添加到包装泛型方法,c#,asp.net-mvc,asp.net-mvc-4,generics,razor,C#,Asp.net Mvc,Asp.net Mvc 4,Generics,Razor,ASP.NET MVC4 razor帮助程序无法将类型参数传递给泛型方法。要解决这个问题,最好的方法可能是添加带有签名的非泛型get方法 public static string Get(Type entityType) 如何添加此包装器,使其在此类中调用泛型Get方法。应该使用反射还是有更好的方法 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] public sealed class Localiza
public static string Get(Type entityType)
如何添加此包装器,使其在此类中调用泛型Get方法。应该使用反射还是有更好的方法
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class LocalizableDisplayNameAttributeI : DisplayNameAttribute
{
public LocalizableDisplayNameAttributeI(string displayName) : base(displayName) { }
public override string DisplayName
{
get
{
return global::Res.Translate(base.DisplayName);
}
}
public static string Get<TEntity>()
{
foreach (LocalizableDisplayNameAttributeI attrib in
typeof(TEntity).GetCustomAttributes(typeof(LocalizableDisplayNameAttributeI), true))
{
return attrib.DisplayName;
}
return typeof(TEntity).Name;
}}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
公共密封类LocalizableDisplayNameAttributeI:DisplayNameAttribute
{
public LocalizableDisplayNameAttributeI(字符串显示名):base(显示名){}
公共重写字符串DisplayName
{
得到
{
返回全局::Res.Translate(base.DisplayName);
}
}
公共静态字符串Get()
{
foreach(本地化显示名称属性属性属性)
typeof(tenty).GetCustomAttributes(typeof(LocalizableDisplayNameAttributeI),true))
{
返回attrib.DisplayName;
}
返回类型(tenty).Name;
}}
反射
return (string)(
typeof(LocalizableDisplayNameAttributeI)
.GetMethod("Get")
.MakeGenericMethod(entityType)
.Invoke(null, null)
);
这个答案只与如何调用泛型方法有关。但是在实际情况中,只需在非泛型版本中再次编写该方法就容易多了。因为您仅在上下文typeof(tenty)
中使用tenty
。因此,您只需搜索typeof(tenty)
并用entityType
替换typeof(tenty)
,即可获得该方法的非泛型版本。反射
return (string)(
typeof(LocalizableDisplayNameAttributeI)
.GetMethod("Get")
.MakeGenericMethod(entityType)
.Invoke(null, null)
);
这个答案只与如何调用泛型方法有关。但是在实际情况中,只需在非泛型版本中再次编写该方法就容易多了。因为您仅在上下文typeof(tenty)
中使用tenty
。因此,您只需搜索typeof(tenty)
并用entityType
替换typeof(tenty)
,即可获得该方法的非泛型版本。反射
return (string)(
typeof(LocalizableDisplayNameAttributeI)
.GetMethod("Get")
.MakeGenericMethod(entityType)
.Invoke(null, null)
);
这个答案只与如何调用泛型方法有关。但是在实际情况中,只需在非泛型版本中再次编写该方法就容易多了。因为您仅在上下文typeof(tenty)
中使用tenty
。因此,您只需搜索typeof(tenty)
并用entityType
替换typeof(tenty)
,即可获得该方法的非泛型版本。反射
return (string)(
typeof(LocalizableDisplayNameAttributeI)
.GetMethod("Get")
.MakeGenericMethod(entityType)
.Invoke(null, null)
);
这个答案只与如何调用泛型方法有关。但是在实际情况中,只需在非泛型版本中再次编写该方法就容易多了。因为您仅在上下文
typeof(tenty)
中使用tenty
。因此,您可以简单地搜索typeof(tenty)
并用entityType
替换typeof(tenty)
以获得该方法的非泛型版本。您可以使用@JeppeStigNielsen已经指出的反射来实现
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class LocalizableDisplayNameAttributeI : DisplayNameAttribute
{
private static readonly MethodInfo _getMethod = typeof(LocalizableDisplayNameAttributeI)
.GetMethods().First(x => x.IsGenericMethod && x.Name == "Get");
public LocalizableDisplayNameAttributeI(string displayName) : base(displayName) { }
public override string DisplayName
{
get
{
return global::Res.Translate(base.DisplayName);
}
}
public static string Get(Type entityType)
{
return (string)_getMethod.MakeGenericMethod(entityType).Invoke(null, null);
}
public static string Get<TEntity>()
{
var attrib = typeof(TEntity)
.GetCustomAttributes(typeof(LocalizableDisplayNameAttributeI), true)
.FirstOrDefault() as LocalizableDisplayNameAttributeI;
return attrib != null ? attrib.DisplayName : typeof(TEntity).Name;
}
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
公共密封类LocalizableDisplayNameAttributeI:DisplayNameAttribute
{
私有静态只读方法信息_getMethod=typeof(LocalizableDisplayNameAttributeI)
.GetMethods().First(x=>x.IsGenericMethod&&x.Name==“Get”);
public LocalizableDisplayNameAttributeI(字符串显示名):base(显示名){}
公共重写字符串DisplayName
{
得到
{
返回全局::Res.Translate(base.DisplayName);
}
}
公共静态字符串Get(类型entityType)
{
return(string)\ u getMethod.MakeGenericMethod(entityType).Invoke(null,null);
}
公共静态字符串Get()
{
var attrib=类型(强度)
.GetCustomAttributes(typeof(LocalizableDisplayNameAttributeI),true)
.FirstOrDefault()作为本地化显示名属性i;
return attrib!=null?attrib.DisplayName:typeof(tenty).Name;
}
}
但似乎您并不真正需要它,因为您没有使用模板参数
您只需执行以下操作:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class LocalizableDisplayNameAttributeI : DisplayNameAttribute
{
public LocalizableDisplayNameAttributeI(string displayName) : base(displayName) { }
public override string DisplayName
{
get
{
return global::Res.Translate(base.DisplayName);
}
}
public static string Get(Type entityType)
{
var attrib = entityType
.GetCustomAttributes(typeof(LocalizableDisplayNameAttributeI), true)
.FirstOrDefault() as LocalizableDisplayNameAttributeI;
return attrib != null ? attrib.DisplayName : entityType.Name;
}
public static string Get<TEntity>()
{
return Get(typeof(TEntity));
}
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
公共密封类LocalizableDisplayNameAttributeI:DisplayNameAttribute
{
public LocalizableDisplayNameAttributeI(字符串显示名):base(显示名){}
公共重写字符串DisplayName
{
得到
{
返回全局::Res.Translate(base.DisplayName);
}
}
公共静态字符串Get(类型entityType)
{
var attrib=entityType
.GetCustomAttributes(typeof(LocalizableDisplayNameAttributeI),true)
.FirstOrDefault()作为本地化显示名属性i;
返回attrib!=null?attrib.DisplayName:entityType.Name;
}
公共静态字符串Get()
{
返回Get(typeof(tenty));
}
}
您可以使用@JeppeStigNielsen已经指出的反射来实现
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class LocalizableDisplayNameAttributeI : DisplayNameAttribute
{
private static readonly MethodInfo _getMethod = typeof(LocalizableDisplayNameAttributeI)
.GetMethods().First(x => x.IsGenericMethod && x.Name == "Get");
public LocalizableDisplayNameAttributeI(string displayName) : base(displayName) { }
public override string DisplayName
{
get
{
return global::Res.Translate(base.DisplayName);
}
}
public static string Get(Type entityType)
{
return (string)_getMethod.MakeGenericMethod(entityType).Invoke(null, null);
}
public static string Get<TEntity>()
{
var attrib = typeof(TEntity)
.GetCustomAttributes(typeof(LocalizableDisplayNameAttributeI), true)
.FirstOrDefault() as LocalizableDisplayNameAttributeI;
return attrib != null ? attrib.DisplayName : typeof(TEntity).Name;
}
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
公共密封类LocalizableDisplayNameAttributeI:DisplayNameAttribute
{
私有静态只读方法信息_getMethod=typeof(LocalizableDisplayNameAttributeI)
.GetMethods().First(x=>x.IsGenericMethod&&x.Name==“Get”);
public LocalizableDisplayNameAttributeI(字符串显示名):base(显示名){}
公共重写字符串DisplayName
{
得到
{
返回全局::Res.Translate(base.DisplayName);
}
}
公共静态字符串Get(类型entityType)
{
return(string)\ u getMethod.MakeGenericMethod(entityType).Invoke(null,null);
}
公共静态字符串Get()
{
var attrib=类型(强度)
.GetCustomAttributes(typeof(LocalizableDisplayNameAttributeI),true)
.FirstOrDefault()作为本地化显示名属性i;
return attrib!=null?attrib.DisplayName:typeof(tenty).Name;
}
}
但似乎您并不真正需要它,因为您没有使用模板参数
您只需执行以下操作:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class LocalizableDisplayNameAttributeI : DisplayNameAttribute
{
public LocalizableDisplayNameAttributeI(string displayName) : base(displayName) { }
public override string DisplayName
{
get
{
return global::Res.Translate(base.DisplayName);
}
}
public static string Get(Type entityType)
{
var attrib = entityType
.GetCustomAttributes(typeof(LocalizableDisplayNameAttributeI), true)
.FirstOrDefault() as LocalizableDisplayNameAttributeI;
return attrib != null ? attrib.DisplayName : entityType.Name;
}
public static string Get<TEntity>()
{
return Get(typeof(TEntity));
}
}
[AttributeUsage(AttributeTargets.Class | attribute