【C#6.0反射:提取只读自动属性的支持字段的名称】教程文章相关的互联网学习教程文章

c# – AssemblyInfo和自定义属性【代码】

我想将自定义属性添加到AssemblyInfo,我创建了一个名为AssemblyMyCustomAttribute的扩展类[AttributeUsage(AttributeTargets.Assembly)] public class AssemblyMyCustomAttribute : Attribute {private string myAttribute;public AssemblyMyCustomAttribute() : this(string.Empty) { }public AssemblyMyCustomAttribute(string txt) { myAttribute = txt; } }然后我在AssemblyInfo.cs中添加了对该类的引用并添加了值// General I...

c# – 如何使两个类属性引用相同的值【代码】

我正在尝试执行以下操作: 我想要两个类Class01和Class02. Class02 Integer的属性使用我的属性Integer of Class01进行初始化.当我改变我的Class01.整数现在我想要我的Class02.整数也发生了变化.我怎样才能做到这一点?Class01 one = new Class01 { Integer = 16 }; Class02 two = new Class02 { Integer = one.Integer };Console.WriteLine("Class one: {0} -- Class two: {1}", one.Integer, two.Integer); // Prints: Class one...

c# – 如何在另一个列表中的两个属性上创建List basead?【代码】

问题 我需要创建一个List< int>从另一个列表中选择两个属性. 例 我有一个课程,有两个我需要的字段.public class MyClass {//Other fieldsint? ValueIdint? ValueTwoId }上面的代码是一个例子,所以不要专注于那里. 我想要检索这些属性,如:myClassList.ElementAt(0).ValueId = 1; myClassList.ElementAt(0).ValueTwoId = 2; myClassList.ElementAt(1).ValueId = 3; myClassList.ElementAt(1).ValueTwoId = 4;List<int> resultList =...

c# – 自动属性初始化IL指令顺序

我想在auto属性上设置默认值,用Fody做一些IL编织. 据我所知,初始化只是一个语法糖,它在构造函数中设置了支持字段.所以我认为使用从最后一个属性的初始化结束到stfld指令的指令创建默认值,该指令设置当前属性的支持字段. 但是,这假设初始化始终是构造函数中的第一件事.这是正确的假设吗?是否有任何边缘情况需要考虑,例如优化?解决方法:我找到了这个名为C#中的Upcoming Features的pdf文件,它描述了C#6的新语言功能. 这是关于auto属...

C#项目不尊重F#type` []`属性【代码】

我有一个简单的F#类型如下:(* Num.fsi *) namespace FsLibmodule Num =[<NoEquality>]type tval of_int : int -> tval to_int : t -> int正如您可能想象的那样,实现是微不足道的,它只是一个案例区别联合与基础int.现在在一个C#项目中相同的解决方案我有以下代码:// CsTool.cs using System; using FsLib;namespace CsTool {class MainClass {public static void Main(string[] args) {Num.t n1 = Num.of_int(1);Num.t n2 = Num.o...

c# – 必须使用适当的属性或方法修改“Content-Type”标头.参数名称:名称【代码】

您好我使用HttpWebRequest GET方法来调用REST服务.我收到错误: – ***’Content-Type’标头必须使用适当的属性或方法进行修改.参数名称:name.***我从stackoverflow检查了所有与此问题相关的答案. 我的代码: – using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;public partial class _Defaul...

c# – 合同的不同属性值【代码】

我有一个主类实现的两个接口.我如何以实现每个合同的方式重构我的代码,每个合同的方法对于诸如DatabaseName之类的参数具有不同的值. 示例: > Class1实现Interface1,Interface2> Interface1.GetData()将DatabaseName设置为Database 1> Interface2.GetData()将DatabaseName设置为Database 2 我可以在方法GetData()中配置这些值,但我想要一个更简洁的方法. 任何模式建议是DI,域驱动,甚至是完成上述操作的基本继承示例都是我正在寻找的...

c# – 获取ICollectionView的Count属性【代码】

我有ICollectionViewprivate ICollectionView _snapshotList;public ICollectionView SnapshotList {get { return _snapshotList; }}我在ViewModel构造函数中进行设置,其中this.smapshotListModel.SnapshotItems返回ObservableCollection_snapshotList = CollectionViewSource.GetDefaultView(this.snapshotListModel.SnapshotItems);_snapshotList.Filter = ErrorMsgFilter;_snapshotList.CollectionChanged += OnCollectionChang...

c# – EF Core:如何将关系添加到shadow属性?【代码】

我有两节课:public class DbLibrary {public Guid Id { get; set; }public string Name { get; set; }public List<DbDepartment> Departments { get; set; } = new List<DbDepartment>(); }public class DbDepartment {public Guid Id { get; set; }public string Name { get; set; } }在这个模型中,我需要DbDepartment没有包含DbLibrary链接的属性.但是我需要在数据库端进行级联删除.为此,我将shadow属性添加到DbDepartment.这是...

c# – 使用TryParse设置对象属性值【代码】

我目前正在重构代码以将Convert.To替换为TryParse. 我遇到了以下一些代码,它们创建并为对象分配属性.List<Person> list = new List<Person>();foreach (DataRow row in dt.Rows) {var p = new Person{ RecordID = Convert.ToInt32(row["ContactID"]) };list.Add(p); }我作为替代品提出的是:var p = new Person { RecordID = Int32.TryParse(row["ContactID"].ToString(), out RecordID) ? RecordID : RecordID };有什么想法,意见,...

c# – 从基类访问应用于派生类中的方法的属性【代码】

所以我有一个案例,我希望能够将属性应用于派生类中的(虚拟)方法,但我希望能够提供一个在我的基类中使用这些属性的默认实现. 我这样做的最初计划是覆盖派生类中的方法,然后调用基本实现,此时应用所需的属性,如下所示:public class Base {[MyAttribute("A Base Value For Testing")]public virtual void GetAttributes() {MethodInfo method = typeof(Base).GetMethod("GetAttributes");Attribute[] attributes = Attribute.GetCust...

c# – 从不同的线程更新对象的不同属性是否安全?【代码】

考虑下面的伪代码.我有一个具有3个属性的类,每个属性并行填充不同的方法. 我是否会遇到从单独的线程填充同一类实例的不同属性的问题?我已经设置了.net fiddle,看起来它运行正常. 如果此代码会导致线程问题,那么在填充属性时我应该使用什么方法来锁定Response类的特定实例?class Response {public string Response1 { get; set; }public string Response2 { get; set; }public string Response3 { get; set; } }void foo() {var r...

c# – Visual Studio Designer不断尝试初始化我的Collection属性【代码】

我创建了一个名为CatalogBrowser的UserControl,它有一个属性ListedFamilies,它公开控件中当前列出的Family对象.这是该物业的代码.public List<Family> ListedFamilies {get {List<Family> returnList = new List<Family>();foreach (Object obj in this.familyObjectListView.Objects) {returnList.Add((Family)obj);}return returnList;}set {this.familyObjectListView.ClearObjects();this.familyObjectListView.Objects = valu...

WinForms(C#)数据绑定对象到Checkbox.Checked属性【代码】

我正在编写一个WinForms应用程序,并试图将.NET对象上的布尔属性绑定到Checkbox的“checked”属性.我成功创建了绑定,但是当我将source属性的值从false更改为true(我有一个切换它的按钮)时,复选框的“checked”属性不会反映该更改.if (chkPreRun.DataBindings["Checked"] == null) {Debug.WriteLine("Adding chkPreRun databinding");Binding _binding = chkPreRun.DataBindings.Add("Checked", NwmConfig, "PreRun")// Added this j...

c# – 为什么SubItems.Clear()也删除了Name属性?【代码】

我在详细模式(.NET 4.0,在Windows 7上运行)中使用WinForms ListView,并且我有一个需要清除特定项目中的子项的函数.不幸的是,当我这样做时,它也清楚了这个名字:item.Name = "TESTNAME"; item.SubItems.Clear(); MessageBox.Show(item.Name); //shows nothing在调试器中我跟踪了它,我看了documentation on MSDN并且它没有用,Clear: Removes all subitems and the parent ListViewItem from the collection.除了ListViewItem仍然非...