我已经检查了一些关于反射和重载方法的其他帖子,但可以找到任何帮助。我发现的一篇文章是this one,但这并没有多大帮助。
我有以下两种方法:
1 | public void Delete<T>(T obj) where T : class { ... }
2 | public void Delete<T>(ICollection<T> obj) where T : class { ... }
Run Code Online (Sandbox Code Playgroud)
我正在尝试获取方法 N°1。
我尝试了经典GetMethod("Delete")方法,但由于有两个具有此名称的方法,因此Ambiguous抛出了-Exception。我尝试使用附加参数指定方法架构,例如GetMethod("Delete", new [] { typeof(Object) })没有找到任何东西(返回空值)。
我想我不妨循环遍历所有方法并检查参数。
我写了以下方法...
public static IEnumerable<MethodInfo> GetMethods(this Type type, String name, Type schemaExclude)
{
IEnumerable<MethodInfo> m = type.GetRuntimeMethods().Where(x => x.Name.Equals(name));
return (from r in m let p = r.GetParameters() where !p.Any(o => schemaExclude.IsAssignableFrom(o.ParameterType)) select r).ToList();
} …Run Code Online (Sandbox Code Playgroud) 我正在序列化作为我的数据实体的类的列表.我有一个包含List的DataProvider.
我总是直接修改集合中的项目.
确定列表中的任何项目是否已更改的最佳方法是什么?我正在使用Compact Framework.
我当前唯一的想法是在加载列表时创建List的哈希值(如果可能的话).然后,当我进行保存时,我重新获取列表的哈希值,看看它们是否是不同的值.如果它们不同,我保存然后更新存储的Hash以便稍后进行比较,如果它们是相同的则我不保存.
有任何想法吗?
使用C#,有没有直接的方法将列表列表(即List<List<T>>)导出到Excel 2003?
我正在解析大文本文件并导出到Excel.一次写一个单元格会产生太多的开销.我选择使用,List<T>以便我不必担心指定行数或列数.
目前,我等到文件结束,然后把我的内容List<List<object>>放到一个二维数组中.然后可以将数组设置为Excel.Range对象的值.它可以工作,但似乎我应该能够获取列表列表,而不必担心行或列的数量,只需将其转储到从A1到任何地方的工作表中.
这是我要替换或改进的代码片段:
object oOpt = System.Reflection.Missing.Value; //for optional arguments
Excel.Application oXL = new Excel.Application();
Excel.Workbooks oWBs = oXL.Workbooks;
Excel._Workbook oWB = oWBs.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel._Worksheet oSheet = (Excel._Worksheet)oWB.ActiveSheet;
int numberOfRows = outputRows.Count;
int numberOfColumns = int.MinValue;
//outputRows is a List<List<object>>
foreach (List<object> outputColumns in outputRows)
{
if (numberOfColumns < outputColumns.Count)
{ numberOfColumns = outputColumns.Count; }
}
Excel.Range oRng = oSheet.get_Range("A1", oSheet.Cells[numberOfRows,numberOfColumns]);
object[,] outputArray = new object[numberOfRows,numberOfColumns];
for (int row = 0; row < …Run Code Online (Sandbox Code Playgroud) 我有一个机器已经处理的日期列表,但它没有包含机器停机的日期.我需要创建一个工作日但没有工作的列表.我不确定这样做的最好方法.我已经开始通过递增范围的所有日期并通过每次迭代整个列表来检查日期是否在列表中.我正在寻找一种更有效的方法来查找日期.
class machineday
{
datetime WorkingDay;
}
class machinedaycollection : List<machineday>
{
public List<TimeCatEvent> GetAllByCat(string cat)
{
_CategoryCode = cat;
List<machineday> li = this.FindAll(delegate(machinedaydummy) { return true; });
li.Sort(sortDate);
return li;
}
int sortDate(machinedayevent1, machinedayevent2)
{
int returnValue = -1;
if (event2.date < event1.date)
{
returnValue = 0;
}
else if (event2.date == event1.date)
{
//descending
returnValue = event1.date.CompareTo(event2.date);
}
return returnValue;
}
}
Run Code Online (Sandbox Code Playgroud) 我正在编写地址簿程序。我将每个人的详细信息存储在List<Person>. 我需要能够按姓氏(如果有关系则使用名字)或邮政编码对这个列表进行排序。
到目前为止,我有这个:
public class Person
{
public string LastName { get; set; }
public string FirstName { get; set; }
public string PostCode { get; set; }
// etc..
}
public class AddressBook
{
public List<Person> People { get; set; }
// asc: ascending or descending
// column: the property to use when sorting
// (in my case either LastName or Postcode)
public void Sort(bool asc, string column)
{
// What should I put here?
}
// …Run Code Online (Sandbox Code Playgroud) 我想知道在C#中构建一个唯一的对象列表是否会更快地遵循一种或另一种模式:
选项1
选项2
在Java中我有一个interface I,一个class C和一个class D,都实现I:
interface I
{
//some method signatures
}
class C implements I
{
//implement methods from I
}
class D implements I
{
//implement methods from I
}
Run Code Online (Sandbox Code Playgroud)
现在我创建一个List持有元素class C,以及第二个List持有元素D:
List<C> c = new LinkedList<C>();
List<D> d = new LinkedList<D>();
Run Code Online (Sandbox Code Playgroud)
我有一个方法,我想应用于所有持有元素实现的列表interface I:
public void modifyList(List<I> l)
{
//call some method defined in I
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试调用该函数时,编译器会抛出一个错误:
public void testModification()
{
modifyList(c);
modifyList(d);
}
==> …Run Code Online (Sandbox Code Playgroud) 我尝试设置List <int>值
List< int > a;
//...
a[i] = X;
Run Code Online (Sandbox Code Playgroud)
ilspy显示设置编译为:
callvirt instance void class [mscorlib]System.Collections.Generic.List`1<int32>::set_Item(int32, !0)
Run Code Online (Sandbox Code Playgroud)
但是这段代码
int[] b;
//...
b[i] = Y;
Run Code Online (Sandbox Code Playgroud)
编译成
stelem.i4
Run Code Online (Sandbox Code Playgroud)
用我的基准测试,它的速度提高了7倍.
据我所知,虚拟通话比stelem更贵.是否可以使用List <T>与数组perfomace
更新
码:
static void Main(string[] args)
{
int N = int.Parse(args[0]);
int M = int.Parse(args[1]);
var sw = new Stopwatch();
sw.Start();
int[] a = new int[N];
for (int k = 0; k < M; ++k)
{
for (int i = 0; i < N; ++i)
{
a[i] = i * 2; …Run Code Online (Sandbox Code Playgroud) 我在从通用列表中获取记录时遇到问题。我创建了一个通用函数,我想从中获取任何类型的类的记录。以下是示例代码:-
public void Test<T>(List<T> rEntity) where T : class
{
object id = 1;
var result = rEntity.Where(x => x.id == id);
}
Run Code Online (Sandbox Code Playgroud)
请建议。提前致谢。
我使用的是Delphi 10.3.1 COMMUNITY版本,在调试项目时无法查看通用的tList。
我知道最新版本的Delphi不支持允许查看通用tList的旧式调试功能。因此,我在以下代码中使用了tList.List来评估tList。
在tList<tRecord>.List我可以看看它,但不能做它tList<Integer>.List。
type
tRecord = record
Field: Integer;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
_Record: tRecord;
_List1: TList<tRecord>;
_List2: TList<Integer>;
i: Integer;
begin
_List1 := TList<tRecord>.Create;
_List2 := TList<Integer>.Create;
for i := 0 to 4 do
begin
_Record.Field := i;
_List1.Add(_Record);
_List2.Add(i);
end;
Caption := IntToStr(_List1.List[0].Field) + IntToStr(_List2.List[0]);
_List1.Free;
_List2.Free;
end;
Run Code Online (Sandbox Code Playgroud)
tList<Integer>在调试过程中如何查看?
generic-list ×10
c# ×8
generics ×4
.net ×2
performance ×2
.net-2.0 ×1
bytecode ×1
c#-2.0 ×1
debugging ×1
delphi ×1
excel ×1
hash ×1
interface ×1
java ×1
linq ×1
list ×1
overloading ×1
reflection ×1
sorting ×1
tlist ×1