我怎么打电话SomeObject.SomeGenericInstanceMethod<T>(T arg)
?
有一些关于调用泛型方法的帖子,但不完全像这个.问题是method参数被约束为泛型参数.
我知道如果签名是相反的
SomeObject.SomeGenericInstanceMethod<T>(string arg)
然后我可以得到MethodInfo
typeof (SomeObject).GetMethod("SomeGenericInstanceMethod", new Type[]{typeof (string)}).MakeGenericMethod(typeof(GenericParameter))
那么,当常规参数是泛型类型时,如何获取MethodInfo?谢谢!
此外,泛型参数可能有也可能没有类型约束.
嗯,我很清楚我的问题的标题太复杂了.我只是想尽可能具体.所以,我会尝试更好地解释问题.
假设我们在一个解决方案中有三个.NET项目.主项目是一个简单的控制台应用程序ApplicationAssembly.该项目引用了另一个托管程序集库DirectlyReferencedLibrary.同时DirectlyReferencedLibrary指的是IndirectlyUsedLibrary.
因此,项目用法看起来像: ApplicationAssembly - > DirectlyReferencedLibrary - > IndirectlyUsedLibrary.
请注意,ApplicationAssembly不直接使用任何声明为IndirectlyUsedLibrary的类型.我们还假设在这些程序集中声明的所有类型都驻留在同一名称空间中.
此解决方案编译并正常工作.
当我在一起时遇到以下情况时会出现问题:
这是一个这样的类的例子.
using System;
namespace Test
{
public static class UnUsedType
{
// It's a generic extension method with a type restriction.
public static void Magic<T>(this T @this)
// It's a type restriction that uses a type from the IndirectlyUsedLibrary.
where T : ProblemType
{
Console.WriteLine("I do nothing actually.");
}
} …
Run Code Online (Sandbox Code Playgroud) 以下方法返回由T
类型元素组成的列表:
public <T> List<T> getList() {
return new ArrayList<T>();
}
Run Code Online (Sandbox Code Playgroud)
在签名中,我们有<T> List<T>
. 这List<T>
是有道理的,因为这是返回值的类型。前面的需要什么<T>
?
没有<T>
和,代码无法编译List<T>
。而忽略了<T>
给
无法解析符号 T
我已经阅读了关于泛型方法的官方 Oracle 教程。它解释说这是语法的一部分:
泛型方法的语法包括一个类型参数列表,在尖括号内,它出现在方法的返回类型之前。
但它并没有真正解释为什么首先需要它或者它对方法有什么确切的影响。
给出以下接口:
interface IEntity
{
int Id{get;}
}
interface IPerson : IEntity
{
string Name{get;}
int Age{get;}
}
interface ITeacher : IPerson
{
string StaffId{get;}
}
interface IStudent : IPerson
{
string StudentId{get;}
string Courses{get;}
}
interface IRepository
{
T Get<T>(int id) where T : IEntity
}
Run Code Online (Sandbox Code Playgroud)
我的命名空间中有以下类
public class EntityBase() : IEntity
{
int Id{get;set;}
}
public class Teacher : EntityBase, ITeacher{}
public class Sudent : EntityBase, IStudent{}
Run Code Online (Sandbox Code Playgroud)
目前我正在实现这个IRepository,如下所示:
class Repository: IRepository
{
IDataContext Context{get;set;}
T Get<T>(int id) where T …
Run Code Online (Sandbox Code Playgroud) 我不知道如何给这个更好的标题,因为我不知道这个模式在Java中被称为什么.
现在我有一个带有此签名的方法:
public Directory getDirectory(Class<? extends Directory> type) { ... }
Run Code Online (Sandbox Code Playgroud)
你这样称呼它:
MyDirectory directory = (MyDirectory)getDirectory(MyDirectory.class);
Run Code Online (Sandbox Code Playgroud)
对类型的约束确保MyDirectory
必须从中派生Directory
.
我真正想要做的是避免强制转换并减少所需的代码量.在C#中你可以说:
MyDirectory directory = getDirectory<MyDirectory>();
Run Code Online (Sandbox Code Playgroud)
有没有办法在Java中做到这一点或类似的东西?我从1.4版开始就没有编写任何Java代码!
我要知道是否可以在一个方法中使用if语句来检查所用泛型的类型.在让我思考这个问题的情况下,我希望以不同的方式处理一个非常基本的Point2D
类和一个Point3D
类.在我需要访问的3D点的情况下Point3d.z
,我不确定这是否会导致问题我想做的伪代码版本是
public <T> void processPoints(T point) {
process(point.x);
process(point.y);
if (T == Point3D) { // What do I do here?
process(point.z); // Will accessing z cause problems?
}
}
Run Code Online (Sandbox Code Playgroud)
实际上,代码process
表示更复杂并且z
依赖于x
,y
因此我正在寻找避免代码重复的方法.我可能会想出一种方法来重载函数,但我很好奇所以我可以学习更多关于泛型的知识.
Java中是否有一种方法可以通过一个方法的声明返回不同的类型?
public Object loadSerialized(String path) {
Object tmpObject;
try {
FileInputStream fis = new FileInputStream(path);
ObjectInputStream ois = new ObjectInputStream(fis);
tmpObject = (Object) ois.readObject();
ois.close();
fis.close();
return tmpObject;
} catch (FileNotFoundException e) {
return null;
} catch (Exception e) {
}
}
Run Code Online (Sandbox Code Playgroud)
我希望该方法返回一个对象,然后在函数调用中将其云转换为正确的类型。那是我的想法,但它不能像这样工作。我需要某种通用的返回类型来做到这一点吗?解决这个问题的最佳方法是什么?
泛型方法返回类型是否基于值引用解析?例如
public class TestGenericMethod
{
public static void main(String[] args)
{
TestGenericMethod dis = new TestGenericMethod();
String str = dis.getFirst(singletonList("String"));
System.out.println("| String ==> " + str);
Integer in = dis.getFirst(singletonList(5));
System.out.println("| Integer ==> " + in);
}
private <T> T getFirst(List<Object> objs)
{
return (T) objs.get(0);
}
}
Run Code Online (Sandbox Code Playgroud)
参考代码片段,此代码编译并正常运行.我的问题在这里,因为我不是特定的类型转换列表对象到我的类型,仍然返回类型兼容String
和Integer
.那么返回类型是在值参考的基础上解决的吗?
我正在尝试编写一个 C# 通用方法,它接受可为空的十进制和双精度值并将它们转换为字符串表示形式。
尽管我正在访问.Value
可空参数,但我收到错误“方法 'ToString' 没有重载需要 1 个参数” 。
这是我的代码。我究竟做错了什么?
public static string ToThousandSeparated<T>(T? value, string naString = "") where T : struct
{
if (value.HasValue)
{
T val = value.Value;
return val.ToString("N0");
}
return naString;
}
Run Code Online (Sandbox Code Playgroud) 假设我有一辆超级汽车,我的子类别是本田、丰田和雷克萨斯。
我有 3 个 LinkedHashMap,使用每个子类作为值:
LinkedHashMap<String, Honda> hondaModels = new LinkedHashMap<String, Honda>();
LinkedHashMap<String, Toyota> toyotaModels = new LinkedHashMap<String, Toyota>();
LinkedHashMap<String, Lexus> lexusModels = new LinkedHashMap<String, Lexus>();
Run Code Online (Sandbox Code Playgroud)
我想要实现的是这样的:
public <T> boolean checkModel(LinkedHashMap<String, <T>> lineup, String model) {
if (lineup.containsKey(model)) {
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
我是否可以通过指定 LinkedHashMap 并使用通用值作为参数来创建方法,这样我就不必为所有 3 个 LinkedHashMap 重复相同的方法?
generic-method ×10
generics ×8
java ×6
c# ×4
.net ×1
casting ×1
methodinfo ×1
reflection ×1
return-type ×1
tostring ×1