Tag: .net 2.0

如何使用Reflection设置类型为List 的Property

已经存在类似的问题,但它似乎没有询问问题所暗示的情况。 用户询问列表中的自定义类,但其列表对象的类型为字符串。 我有一个类Foo,它有一个Bars列表: public class Foo : FooBase { public List bars {get; set;} public Foo() {} } public class Bar { public byte Id { get; set; } public byte Status { get; set; } public byte Type { get; set; } public Bar(){} } 我通过Activator.CreateInstance()使用reflection实例化Foo。 现在我需要使用Bar对象填充该条形列表。 Foo是使用获得的 Assembly.GetAssembly(FooBase).GetTypes().Where(type => type.IsSubclassOf(FooBase)); 酒吧是同一个大会的公共课。 我需要以某种方式达到那种类型。 我似乎无法看到Foo中包含的列表类型是什么。 […]

可序列化的类

如何让Student class可序列化? 我正在读这篇文章,但如果我在下面的场景中实现它,我不知道什么是正确的方法。 public class Student { private string _studentNumber; private string _lastName; private string _firtName; private List _subjects; public Student() { } public string StudentNumber { get { return _studentNumber; } set { _studentNumber = value; } } public string LastName { get { return _lastName; } set { _lastName = value; } } […]

.net资源DLL包含多个resx文件

我需要知道从具有多个资源文件(.resx)的资源DLL中正确访问字符串和图像的方法。 我有一个非常大的项目,我正在研究,它由几个winforms组成,每个winforms有多个面板。 我试图通过使每个表单具有自己的res文件并将应用程序限制为一个资源DLL而不是多个资源DLL来整理一些东西。 问题是当我尝试将新资源文件添加到我的资源DLL并尝试访问字符串时,例如,正在保存在新创建的resx文件中。 ResourceManager返回一个空字符串。 这是我如何创建我的ResourceManager对象并获取字符串 static public string GetResourceString(string sStringName) { System.Reflection.Assembly myDllAssembly = System.Reflection.Assembly.LoadFile(ResPath); string[] allResourcesinDLL = myDllAssembly.GetManifestResourceNames(); ResourceManager ResInstance = new ResourceManager(allResourcesinDLL[0].Replace(“.resources”, string.Empty), myDllAssembly); return ResInstance.GetString(sStringName); } 其中ResPath是我的资源DLL的路径,sStringName是资源/字符串标题。 我不确定是否有办法指定要读取的resx文件或是否应以其他方式处理它。 请记住,我的应用程序是使用.net 2.0框架编译的,所以我只限于我能够使用的API。

查找特定格式的出现次数字符串在给定文本中出现

我有一个大字符串,其中可以有不止一次出现的特定单词(文本后跟单个冒号,如“test:”)。 例如,像这样: word: TEST: word: TEST: TEST: // random text “word”出现两次,“TEST”出现三次,但数量可以变化。 此外,这些单词不必具有相同的顺序,并且可以在与该单词相同的行中有更多的文本(如“TEST”的最后一个示例所示)。 我需要做的是将出现次数附加到每个单词,例如输出字符串需要是这样的: word_ONE: TEST_ONE: word_TWO: TEST_TWO: TEST_THREE: // random text 用于获取我写的这些单词的RegEx是^\b[A-Za-z0-9_]{4,}\b: . 但是,我不知道如何快速完成上述工作。 有任何想法吗?

将多个列表绑定到DataGridView

我有两个静态列表,它们将绑定到DataGridView以及两个按钮字段。 我只能绑定一个列表和一个按钮字段。 是否可以将多个列表绑定到DataGridView ? ServiceController objSAVAdminService = new ServiceController(“SAVAdminService”); ServiceController objSAVService = new ServiceController(“SAVService”); ServiceController objPPVService = new ServiceController(“PPVService”); ServiceController objMLLService = new ServiceController(“MLLService”); List ServiceName = new List(); List ServiceStatus = new List(); ServiceName.Add(objSAVService.ServiceName.ToString()); ServiceName.Add(objSAVAdminService.ServiceName.ToString()); ServiceName.Add(objPPVService.ServiceName.ToString()); ServiceName.Add(objMLLService.ServiceName.ToString()); ServiceStatus.Add( objSAVService.Status.ToString()); ServiceStatus.Add( objSAVAdminService.Status.ToString()); ServiceStatus.Add( objPPVService.Status.ToString()); ServiceStatus.Add( objMLLService.Status.ToString()); 我需要将这两个列表绑定到DataGridView并且每行上还有两个按钮来启动或停止相应的服务。

使用.Net UserControl设计问题

我创建了一个UserControl,里面有一个ListView 。 ListView可通过属性公开访问。 当我将UserControl放在一个表单中并尝试通过属性设计ListView , ListView保持这种状态,直到我再次编译并恢复到默认状态。 如何让我的设计更改坚持ListView ?

防止生成Form.resx

我有一个从另一个inheritance的表单。 在设计器中,每当我调整控件大小或位置等内容时,VS都会自动为该表单生成一个resx文件。 resx只包含一些combobox的KeyValuePairs,因为这些值已在父类中定义,所以w / c实际上是不必要的。 除此之外,designer.cs还获得更新w /inheritance属性,如Text,NumericUpDown.Value,DisplayMember,ValueMember等,w / c再次已经在父类中定义。 我知道designer.cs应该是新的位置和大小更新,但我不希望它更新从父类inheritance的其他东西。 有没有办法防止这种情况,让设计师更新位置和大小? 更新:我发现由于OnLoad()中的绑定逻辑,ComboBox.Items.AddRange()被添加到Designer.cs和.resx文件中。 这主要是为了防止设计人员在Designer上重新加载表单时正确加载。 我修改了OnLoad只在DesignMode为false时才运行绑定逻辑。 重新加载设计器时,表单不再抛出错误,但每当我通过属性对话框更改任何属性时,某些控件属性仍然不必要地添加回Designer.cs。 UPDATE2:通过在此处应用建议,完全阻止了设计者生成不必要的控件属性。 现在生成的只是控制位置和大小。

DataGridView绑定问题:“索引-1没有值。”

我有一个datagridview绑定到绑定源和窗体上的几个按钮。 一个按钮将一个项目添加到绑定源,另一个按钮删除当前选定的项目。 还有一个事件处理程序,它侦听CurrentChanged事件并更新“删除”按钮的“已启用”状态。 在我从datagridview中删除最后一项之前,所有东西都是笨拙的。 然后我看到一个非常丑陋的例外: at System.Windows.Forms.CurrencyManager.get_Item(Int32 index) at System.Windows.Forms.CurrencyManager.get_Current() at System.Windows.Forms.DataGridView.DataGridViewDataConnection.OnRowEnter(DataGridViewCellEventArgs e) at System.Windows.Forms.DataGridView.OnRowEnter(DataGridViewCell& dataGridViewCell, Int32 columnIndex, Int32 rowIndex, Boolean canCreateNewRow, Boolean validationFailureOccurred) at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick) at System.Windows.Forms.DataGridView.SetAndSelectCurrentCellAddress(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick, Boolean clearSelection, Boolean forceCurrentCellSelection)\r\n at System.Windows.Forms.DataGridView.MakeFirstDisplayedCellCurrentCell(Boolean includeNewRow) at System.Windows.Forms.DataGridView.OnEnter(EventArgs […]

如何序列化到dateTime

努力获取任何时区的DateTimes。 我正在使用DateTimeOffset,一个字符串和一个XmlElement属性。 当我这样做时,我收到以下错误: [InvalidOperationException:’dateTime’是XmlElementAttribute.DataType属性的无效值。 dateTime无法转换为System.String。] System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model,String ns,ImportContext context,String dataType,XmlAttributes a,Boolean repeats,Boolean openModel,RecursionLimiter limiter)+450 [InvalidOperationException:反映类型’System.String’的错误。] System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model,String ns,ImportContext context,String dataType,XmlAttributes a,Boolean repeats,Boolean openModel,RecursionLimiter limiter)+1621 System.Xml.Serialization.XmlReflectionImporter.ImportAccessorMapping(MemberMapping访问器,FieldModel模型,XmlAttributes a,String ns,Type choiceIdentifierType,Boolean rpc,Boolean openModel,RecursionLimiter limiter)+8750 System.Xml.Serialization.XmlReflectionImporter.ImportFieldMapping(StructModel parent,FieldModel model,XmlAttributes a,String ns,RecursionLimiter limiter)+139 System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping,StructModel model,Boolean openModel,String typeName,RecursionLimiter limiter)+1273 [InvalidOperationException:反映属性’creationTimeX’时出错。] … 码: [System.Xml.Serialization.XmlElement(ElementName = “creationTime”, DataType=”dateTime”)] public string creationTimeX { get […]

如何使用reflection将Int16值设置为Nullable 属性?

我需要这样做: – MyClass.cs using System; using System.Collections.Generic; using System.Text; namespace set_property__Enum_To_Nullable_Enum { public enum MyEnum : short { One, Two, Three } public class MyClass { public string MyStringProperty { get; set; } public MyEnum? MyEnumProperty { get; set; } public void ShowAll(string message) { Console.WriteLine(message); Console.WriteLine(“String = ” + MyStringProperty); Console.WriteLine(“Enum = ” + […]