我正在尝试使用MethodInfo MakeGenericMethod,如下所示:
foreach (var type in types)
{
object output = null;
var method = typeof (ContentTypeResolver).GetMethod("TryConstruct");
var genmethod = method.MakeGenericMethod(type);
var arr = new object[] { from, output };
if ((bool)genmethod.Invoke(null, arr))
return (IThingy)arr[1];
}
Run Code Online (Sandbox Code Playgroud)
针对以下通用方法:
public static bool TryConstruct<T>(string from, out IThingy result) where T : IThingy, new()
{
var thing = new T();
return thingTryConstructFrom(from, out result);
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是我在MakeGenericMethod行上得到了一个争论异常,因为我传递的类型不是'new()'
这有什么办法?谢谢
我有这样的方法,
public List<T> Test<T>()
{
// do something.
}
Run Code Online (Sandbox Code Playgroud)
我不知道T是什么,也没有.但我的T型为TYPE.
例如:
class Person
{
}
var type = typeof(Person);
Run Code Online (Sandbox Code Playgroud)
我没有人.人在保持类型对象.
我该如何使用测试方法?
var list = Test<type>(); // It gives an error like this. I must use the type object.
Run Code Online (Sandbox Code Playgroud) 我正在尝试包装spray-json解析器,使其返回Option而不是抛出异常.
作为第一步,我只是尝试用自己的方法包装方法,但是我在使它成为通用方法时遇到了问题.
解析器使用隐式格式对象(为我正在使用的具体类型定义)但是当该方法是通用的时,编译器会抱怨:
[error] Cannot find JsonReader or JsonFormat type class for T
[error] def parse[T](s: String): T = JsonParser(s).convertTo[T]
Run Code Online (Sandbox Code Playgroud)
这是相关的代码:
case class Person(name: String)
object Protocols {
implicit val personFormat = jsonFormat1(Person)
}
import spray.json._
object Parser {
import com.rsslldnphy.json.Protocols._
// JsonParser(s).convertTo[Person] works fine, but..
def parse[T](s: String): T = JsonParser(s).convertTo[T] // .. doesn't
}
Run Code Online (Sandbox Code Playgroud)
我需要做些什么才能让它发挥作用?
我有一个通用方法,我们可以将T作为接口类型传递.方法返回对应T类型的数据列表.我对此方法有20-25条相同的条件如何优化逻辑.
类实现接口.示例Student类实现IStudent接口.
public ObservableCollection<T> GetAll<T>()
{
try
{
if (typeof(T) == typeof(IStudent))
{
return GetAll<T, Student>();
}
else if (typeof(T) == typeof(IZone))
{
return GetAll<T, Zone>();
}
else if (typeof(T) == typeof(IEmployee))
{
return GetAll<T, Employee>();
}
else if (typeof(T) == typeof(ICourse))
{
return GetAll<T, Course>();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这里调用者传递接口类型T和I检查T的类型.我传递给其他函数T和将返回T的列表的类.其他函数在基类中我无法改变.有谁能建议我一些相同的东西.
我试图使用foreach循环的泛型方法,它将传递不同的参数作为参数.
在下面的这个例子中,我想传递不同的参数(EmployeeDisplayOrder或EmployeeEnrollOrder)
public void SaveEmployeeDisplayOrder(ICollection<Employee> employees)
{
//some code
foreach( var emp in employees)
{
UpdateSpecificEmployeeOrder(employee.id, e => e.EmployeeDisplayOrder);
}
}
public void SaveEmployeeEnrollOrder(ICollection<Employee> employees)
{
//some code
foreach( var emp in employees)
{
UpdateSpecificEmployeeOrder(employee.id, e => e.EmployeeEnrollOrder);
}
}
Run Code Online (Sandbox Code Playgroud)
我想要这样的东西
public void UpdateEmployeeOrders(ICollection<Employee> employee)
{
foreach( var emp in employees)
{
UpdateSpecificEmployeeOrder(employee.id, //generic property);
}
}
Run Code Online (Sandbox Code Playgroud)
并从SaveEmployeeDisplayOrder和SaveEmployeeEnrollOrder调用此UpdateEmployeeOrders泛型方法.
UpdateSpecificEmployeeOrder的方法签名
UpdateSpecificEmployeeOrder( int employeeid, params Expression<Func<Employee, object>>[] property)
Run Code Online (Sandbox Code Playgroud)
这可能吗?
我正在使用泛型方法进行一些测试,我想在下面转换这两个方法(convertFloatListToArray和convertShortListToArray)(convertListToArray):
public class Helper{
public static float[] convertFloatListToArray(List<Float> list){
float[] array = new float[list.size()];
for(int i = 0; i<list.size(); i++){
array[i] = list.get(i);
}
return array;
}
public static short[] convertShortListToArray(List<Short> list){
short[] array = new short[list.size()];
for(int i = 0; i<list.size(); i++){
array[i] = list.get(i);
}
return array;
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用泛型时,如下所示,我有一些错误:
public class Helper{
public static <T, E> T convertListToArray(List<E> list){
T array = new T[list.size()];
for(int i = 0; i<list.size(); i++){
array[i] = list.get(i);
}
return array;
} …Run Code Online (Sandbox Code Playgroud) 我怎样才能使这个方法通用?我应该如何将 Stock_ID 反映到此方法中? 提前致谢
public class LocalSQL<T> where T : new()
{
public static async Task<List<T>> GETLAST( )
{
return await conn.Table<T>()
.OrderByDescending(x => x.Stock_ID)
.Take(10)
.ToListAsync();
}
}
Run Code Online (Sandbox Code Playgroud) public IList GetClientsByListofID(IList ids)其中T:IClient {IList clients = new List(); clients.Add(new Client(3)); }
我在这里得到一个编译器错误:
无法从'Bailey.Objects.Client'转换为'T'
客户端对象实现IClient接口.我的目标是尝试放松我的课程之间的耦合(学习DI的东西).我想我可以说它可以使用任何类型的客户端对象,并将返回.
我完全不在这里吗?
谢谢
乔恩霍金斯
我有下面的通用方法,如果它工作,它将服务于它的目的!但是items.Add(new T(mo));部分不会编译因为我使用构造函数.有人可以帮忙吗?
private List<T> Items<T>(string query) where T : new()
{
List<T> items = new List<T>();
ManagementObjectCollection moc = new ManagementObjectSearcher(query).Get();
foreach (ManagementObject mo in moc)
items.Add(new T(mo));
return items;
}
Run Code Online (Sandbox Code Playgroud) ArrayList<ShipDetail> detailArray = new ArrayList<ShipDetail>(Arrays.asList(shipDetail));
Sorter.QuickSort( detailArray );
Run Code Online (Sandbox Code Playgroud)
这是我的 Sorter 类,我试图在其中实现一些算法。
public class Sorter
{
public static<T extends Comparable<T>> void QuickSort(AbstractList<T> collection )
{
quickSort(collection,0,collection.size()-1);
}
}
Run Code Online (Sandbox Code Playgroud)
但是在编译时我收到以下错误:
必需:AbstractList 找到:ArrayList 原因:推理变量 T 具有不兼容的边界等式约束:ShipDetail 上限:Comparable,其中 T 是类型变量:T 扩展在方法 QuickSort(AbstractList) 中声明的 Comparable
generic-method ×10
c# ×7
generics ×6
java ×2
.net ×1
arraylist ×1
arrays ×1
c#-4.0 ×1
class ×1
collections ×1
foreach ×1
logic ×1
reflection ×1
scala ×1
spray-json ×1