Tag: casting

通用列表为IEnumerable

我正在尝试将List转换为IEnumerable,因此我可以validation不同的列表不为null或为空: 假设myList是List 。 然后在我想要的来电代码中: Validator.VerifyNotNullOrEmpty(myList as IEnumerable, @”myList”, @”ClassName.MethodName”); 有价值的代码是: public static void VerifyNotNullOrEmpty(IEnumerable theIEnumerable, string theIEnumerableName, string theVerifyingPosition) { string errMsg = theVerifyingPosition + ” ” + theIEnumerableName; if (theIEnumerable == null) { errMsg += @” is null”; Debug.Assert(false); throw new ApplicationException(errMsg); } else if (theIEnumerable.Count() == 0) { errMsg += @” is empty”; […]

如何将DbSet 转换为List

鉴于以下简化的Entity Framework 6上下文,我尝试使用实体填充List,但是如何通过reflection进行转换(我相信)有问题。 public class FooContext : DbContext { public virtual IDbSet Foo { get; set; } //… } public class FooClass { public int Id{ get; set; } public string Name {get; set; } //… } public main() { using (var context = new FooContext()) { var sets = typeof(FooContext).GetProperties().Where(pi => pi.PropertyType.IsInterface && pi.PropertyType.GetGenericTypeDefinition().ToString().ToLower().Contains(“idbset”)); […]

为什么我必须在C#中将枚举转换为int?

这是我的代码: internal enum WindowsMessagesFlags { WM_EXITSIZEMOVE = 0x00000232, WM_DISPLAYCHANGE = 0x0000007e, WM_MOVING = 0x00000216, } protected override void WndProc(ref Message m) { switch(m.Msg) { case (int)WindowsMessagesFlags.WM_DISPLAYCHANGE: FixWindowSnapping(); break; case (int)WindowsMessagesFlags.WM_EXITSIZEMOVE: SaveWindowProperties(); break; case (int)WindowsMessagesFlags.WM_MOVING: KeepProperLocation(ref m); break; } } 反正有没有阻止铸造?

IQueryable to ObservableCollection 其中a =匿名类型

我希望将listview的datacontext绑定到可观察的集合。 现在我有: // CurrentEmploye = some employee Entities.DatabaseModel m = new Entities.DatabaseModel(); var q = from t in m.TimeSheet join emp in m.Employees on t.idEmployee equals emp.id where emp.id == CurrentEmploye.id select new { firstName = emp.firstName, lastName = emp.lastName, position = emp.position, clockInDate = t.clockInDate, clockOutDate = t.clockOutDate, }; listView1.DataContext = q; 该代码正确填充列表视图。 现在,每当我更新listview项时,我都想更新listview。 […]

将0.0转换为双倍有什么问题?

当我尝试将0.0转换为double时,我有InvalidCastException ,为什么会这样? 我做(float)value时很好。

字符串强制转换

为什么有这样的方法可以转换为.net中的字符串? 我看到的方法是.ToString,Convert.ToString()和(string)。 有什么不同。

在C#中将超出范围的数字转换为枚举不会产生exception

以下代码不会产生exception,而是将值4传递给tst。 任何人都可以解释这背后的原因吗? public enum testing { a = 1, b = 2, c = 3 } testing tst = (testing)(4);

有没有办法将对象强制转换回原始类型而不指定每个案例?

我有一个不同类型对象的数组,我使用BinaryWriter将每个项目转换为二进制等效项,以便我可以通过网络发送结构。 我现在做的事情 for ( i=0;i<tmpArrayList.Count;i++) { object x=tmpArrayList[i]; if (x.GetType() == typeof(byte)) { wrt.Write((byte)x); } …….. 问题是如果错过了一个类型,我的代码将来可能会破坏。 我想做点什么。 object x=tmpArrayList[i]; wrt.Write(x); 但除非我每次演员,否则它不起作用。 编辑: 在查阅答案之后,这就是我想出的function。 为了测试,该函数将数组发送到syslog。 private void TxMsg(ArrayList TxArray,IPAddress ipaddress) { Byte[] txbuf=new Byte[0]; int sz=0; // caculate size of txbuf foreach (Object o in TxArray) { if ( o is String ) { sz+=((String)(o)).Length; […]

c#编译器是否会执行多个隐式转换以从一种类型转换到另一种类型?

假设您有自己的类如下: public sealed class StringToInt { private string _myString; private StringToInt(string value) { _myString = value; } public static implicit operator int(StringToInt obj) { return Convert.ToInt32(obj._myString); } public static implicit operator string(StringToInt obj) { return obj._myString; } public static implicit operator StringToInt(string obj) { return new StringToInt(obj); } public static implicit operator StringToInt(int obj) { […]

将文本文件转换为List

我有一个ID号的CSV文件,我需要它作为IEnumberable 。 我试图尽可能简单地从A到B,并想出了以下想法: Global.migrated = File.ReadAllText(Global.migrationList).Split(‘,’).Cast().ToList(); 但是当我运行它时,我被告知Cast无效。 我也尝试读取String列表并转换为int: List myTemp = File.ReadAllText(Global.migrationList).Split(‘,’).ToList(); Global.migrated = myTemp.Cast(); 但是这会引发以下错误: Cannot implicitly convert type ‘System.Collections.Generic.IEnumerable’ to ‘System.Collections.List.’ An explicit conversion exists. Are you missing a cast? 我不明白。 在这种情况下,我试图强制转换的对象不是IEnumerable 。 它应该是List 。 我担心我错过了一些关于铸造如何工作的重要信息。