我有3个不同的类,一个通用的"实体"类,然后是两个继承它的类,一个子弹类和一个敌人类.
然后我有一个实体类的列表,列表中的子弹和敌人,以及我对它们进行相同处理的很多地方.但是,有时候,我想循环通过其中一个类,而不是其他类,例如循环通过每个子弹,而不是敌人.有没有办法做到这一点?
我试过了
foreach (Bullet tempBullet in entities)
Run Code Online (Sandbox Code Playgroud)
但是我得到了错误
无法将'Enemy'类型的对象强制转换为'Bullet'.
任何人都知道这是否可行,还是我必须使用单独的列表?
我无法获取当前行值。我能怎么做?
Run Code Online (Sandbox Code Playgroud)bindingSource1.DataSource = LData; (LData is Generic List)
public DataRow currentRow
{
get
{
int position = this.BindingContext[bindingSource1].Position;
if (position > -1)
{
return ((DataRowView)bindingSource1.Current).Row;
}
else
{
return null;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我无法获得当前行:
MessageBox.Show(currentRow["NAME"].ToString());
Run Code Online (Sandbox Code Playgroud)
出现错误:InvalidCastException,我该怎么办?谢谢。
我有一个具有指定初始容量的列表:
private void Foo()
{
List<double> myList = new List<double>(1024);
myList.Add(1.0);
myList.Add(2.0);
....
myList.Clear(); <-- what is the list's 'initial' capacity now?
}
Run Code Online (Sandbox Code Playgroud)
myList打电话后会有什么能力myList.Reset()?是1024吗?
我有一个 KeyValuePair 列表(Key 是 string 类型,value 是 int 类型)。我想返回键值对列表,它将获得我的匹配条件示例:返回一个列表,其中 Key = "A" 和 Value = 10。
select 将如何处理此问题以及如何编写表达式以获得所需的结果?
我可以Generic.List用我的类的实例填充 a ,每个实例在 ID 属性中都有一个唯一的值,就像这样。
$packages = [System.Collections.Generic.List[object]]::New()
class Package {
# Properties
[String]$ID
# Constructor
Package ([String]$id) {
$this.ID = $id
}
}
foreach ($i in 65..90) {
$packages.Add([Package]::New("$([char]$i)"))
}
Run Code Online (Sandbox Code Playgroud)
现在我想获取特定项目的索引,沿着$packages.IndexOf('C'). 但是在 C# 中这样做似乎需要使用 lambda,而 PowerShell 似乎不支持。我宁愿不必初始化我自己的索引变量并遍历列表中的所有项目,检查它们的 ID 并在此过程中增加索引。但是,也许这就是仅使用带有本机 PS cmdlet 的 PowerShell 和仅使用 C# 之间的灰色区域,您在这两种极端情况下都错过了糖,而必须自己制作?
我有一个自定义DTO的列表,我试图传递给WCF服务.
我收到以下错误:
尝试序列化参数tcp:// localhost /:oObject时出错.InnerException消息是'Type'System.Collections.Generic.List`1 [[TEGE.ER.WorkFlowEngine.WFCommon.HeartBeat.HeartBeatDTO,WFCommon,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]]'with data合同名称'ArrayOfHeartBeatDTO:TEGE.ER.WorkFlowEngine.WFCommon.HeartBeat'不是预期的.将任何静态未知的类型添加到已知类型列表中 - 例如,通过使用KnownTypeAttribute属性或将它们添加到传递给DataContractSerializer的已知类型列表中.有关更多详细信息,请参阅InnerException.
我有DTO DataContract和DataMember集.我做了一个自动WCF服务引用,并确保执行高级选项将集合更改为通用列表而不是数组.
我错过了什么?经过很多做类似事情的网站,却无法让我的工作过去这个错误.有任何想法吗?
发件人代码:
<DataContract(Name:="HeartBeatDTO", Namespace:="TEGE.ER.WorkFlowEngine.WFCommon.HeartBeat")> _
Public Class HeartBeatDTO
Public Sub IssuePatientReport()
SBClient.SendCommunication(_PatientID, _HeartBeats)
End Sub
Run Code Online (Sandbox Code Playgroud)
接收方代码:
Public Sub SendCommunication(ByVal sKey As String, ByVal oObject As Object) Implements iOperatorCommunication.SendCommunication
If _CurrentCommunicationLog.ContainsKey(sKey) Then
_CurrentCommunicationLog.Item(sKey) = oObject
Else
_CurrentCommunicationLog.Add(sKey, oObject)
End If
End Sub
Run Code Online (Sandbox Code Playgroud)
我的界面:
<ServiceContract(Namespace:="tcp://localhost/")> _
<ServiceKnownType(GetType(List(Of HeartBeatDTO)))> _
Public Interface iOperatorCommunication
<OperationContract()> _
Function ReceiveCommunication(ByVal sKey As String) As Object
<OperationContract()> _
Function ReturnCommunicationLevel() …Run Code Online (Sandbox Code Playgroud) 我有一个有一个List<String>或多个的课程List<SomeObject>.
我得到了属性的类型:
propertyClass = PropertyUtils.getPropertyType(currentObject, property);
我想要做的是检查propertyClass是否为a List<SomeType>并获取列表中类型的类对象.在此之后,我将要创建ArrayList给定类型的一个并填充该类型的对象(所有动态创建,我将使用它来动态加载文件中的一些数据).
我可以使用反射来做到这一点吗?
我<asp:Label> 希望根据我传递给它的Generic对象进行填充.
目前我有以下代码: -
private void PopulateEnglishQuestion(int questionId)
{
ReportQuestion reportQuestion = questionsBll.GetReportQuestions().Where(x=> x.ReportQuestionId == questionId).FirstOrDefault();
PopulateLabels(reportQuestion);
}
private void PopulateTranslatedQuesiton(int questionId)
{
ReportQuestionTranslation reportQuestionTranslation = questionsBll.GetReportQuestionsTranslation().Where(x => x.QuestionId == questionId).FirstOrDefault();
PopulateLabels(reportQuestionTranslation);
}
private void PopulateLabels<T>(T item)
{
lblQuestionTitle.Text = typeof (T) == typeof (ReportQuestion)
? ((ReportQuestion) (item)).ReportQuestionTitle
: ((ReportQuestionTranslation) (item)).ReportQuestionTitleTrans;
}
Run Code Online (Sandbox Code Playgroud)
如何让PopulateLabels方法正常工作?
我有这个LINQ从通用列表中获取特定类成员的不同值:
var distinctDescriptions = itemsForMonthYearList.Select(x => x.ItemDescription).Distinct();
Run Code Online (Sandbox Code Playgroud)
通用列表以这种方式定义:
List<ItemsForMonthYear> itemsForMonthYearList;
Run Code Online (Sandbox Code Playgroud)
这堂课是:
public class ItemsForMonthYear
{
public String ItemDescription { get; set; }
public String monthYr { get; set; }
public int TotalPackages { get; set; }
public Decimal TotalPurchases { get; set; }
public Decimal AveragePrice { get; set; }
public Double PercentOfTotal { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我认为这会奏效:
var distinctDescriptions = itemsForMonthYearList.Select(x => x.ItemDescription).Distinct().OrderBy(x => x.ItemDescription);
Run Code Online (Sandbox Code Playgroud)
...但它甚至没有编译:
" 'string'不包含'ItemDescription'的定义,也没有扩展方法'ItemDescription'接受'string'类型的第一个参数(你是否缺少using指令或汇编引用?) "
如何按字母顺序对不同的值进行排序?
我的对象定义如下:
public class ModelList<T> : ModelBase, IModelList<T>, IModelList
where T : IModelListItem, new()
{
public void Method1()
{
// do work here!
}
}
public class Object1 : ModelListItem
{
}
public class Object2 : ModelListItem
{
}
public class Objects1: ModelList<Object1>, IModelList
{
}
public class Objects2: ModelList<Object2>, IModelList
{
}
Run Code Online (Sandbox Code Playgroud)
在远处代码的某处,我有一个方法将接收Objects1或Objects2的集合对象.有没有办法从这里调用Method1?
private void DoSomething(object O)
{
// O can be either Objects1 or Objects2
O.Method1();
}
Run Code Online (Sandbox Code Playgroud) generic-list ×10
c# ×7
generics ×3
list ×2
asp.net ×1
datasource ×1
foreach ×1
indexof ×1
java ×1
linq ×1
powershell ×1
properties ×1
reflection ×1
wcf ×1