Tag: 属性

如何检查类型是否标有属性?

它需要反思吗?

访问.aspx文件中的属性设置值

我能够在我的类文件SomeClass.cs中访问Settings.settings文件中定义的属性的值: TestProject.Properties.Settings property = Properties.Settings.Default; string myValue = property.someValue; 但是,我想从Default.aspx页面访问属性值。 我试过了: 它在属性右侧给出错误:“当前上下文中不存在名称属性。” 是否可以从.aspx文件中访问属性项? 我能想到的唯一选择是创建一个只读取属性项的.cs类,并提供.aspx文件可以使用的getter。

数组属性初始化

如何初始化数组属性? 试过这个: feeds = new List(); feeds.Add(new Feed() { Names = { “Cluj Approach”, “Cluj Tower” }, Frequencies = { 117.25 } }); 使用Feed类: class Feed { public string[] Names { get; set; } public float[] Frequencies { get; set; } public float Latitude { get; set; } public float Longitude { get; set; } […]

无法获取PropertyInfo.SetValue()来设置我的对象上的值

我已将下面的代码简化为一个基本示例,但我仍然无法获取要设置的值。 执行propertyInfo.SetValue()时,它将在我的Contact对象的setter上命中断点,并在’setter’中正确设置该值。 但是,在执行SetValue()之后,projectContact.Contact对象上的字符串属性尚未更改为“a”。 知道我在这里做错了什么吗? IEnumerable contacts = GetContactsByProject(projectId); foreach (ProjectContact projectContact in contacts) { foreach (PropertyInfo propertyInfo in projectContact.Contact.GetType().GetProperties()) { if (propertyInfo.PropertyType == typeof(string)) { propertyInfo.SetValue(projectContact.Contact, “a”, null); } } }

为什么List的副本仍使用C#更改原始List中的属性

可以说我有这门课 public class Employee { public string FirstName { get; set; } public string LastName { get; set; } public bool isActive { get; set; } } 并像这样使用它: List Employees = new List(); Employees.Add(new Employee { FirstName = “firstname”, LastName = “lastname”, isActive = true }); List EmployeesCopy = new List(Employees); EmployeesCopy[0].isActive = false; 为什么更改EmployeesCopy […]

如何获取字段的自定义属性值?

我正在使用FileHelpers写出固定长度的文件。 public class MyFileLayout { [FieldFixedLength(2)] private string prefix; [FieldFixedLength(12)] private string customerName; public string CustomerName { set { this.customerName= value; **Here I require to get the customerName’s FieldFixedLength attribute value** } } } 如上所示,我想访问属性的set方法中的自定义属性值。 我该如何实现这一目标?

在Designer中:设置属性默认值,但在设置默认值时不调用属性?

我有一段代码如下: [DefaultValue(false)] public bool Property { set { blah = value; someControl.Visible = value; } get { return blah; } } 当我查看设计器中的属性时,属性设置为false(如果我使用它,则为true)。 但该物业并未实际设定。 控件的Visible值不会更改。 如何让设计师实际使用默认值设置属性?

为什么属性隐藏具有相同名称的Inherited函数?

我有一个类Foo其中包含一个函数myName 。 类bar从Fooinheritance并具有一个也称为myName的Property 我认为这应该没问题,因为: 该属性将编译为get_myName() (因此不会与Foo::myName(int,int)发生冲突) Foo::myName有参数,显然,属性没有(再次,没有colision)。 然而我仍然得到警告: ‘Bar.myName’ hides inherited member ‘Foo.myName(int, int)’. Use the new keyword if hiding was intended. 示例代码: public class Foo { public int myName(int a, int b) { return 0; } } class Bar: Foo { int a; int b; int myName { get { return a + b; […]

访问您自己类中的成员:是否使用(自动)属性?

我已经将这个“问题”创建为社区维基,因为没有正确或错误的答案。 我只想知道社区对这个具体问题的看法。 当你有一个带有实例变量的类,并且你还创建了只是这些实例变量的getter和setter的属性时,你应该使用你自己的类中的属性,还是应该总是使用实例变量? 在C#3.0中拥有自动属性使得这个决定变得更加困难。 使用属性: public class MyClass { private string _name; // could be an auto-property of-course public string Name { get { return _name; } set { _name = value; } } public void Action() { string localVar = Name; // … Name = “someValue”; // … } } 使用实例变量: public class MyClass […]

在初始化列表中分配readonly属性

可以告诉我,为什么它会编译? namespace ManagedConsoleSketchbook { public interface IMyInterface { int IntfProp { get; set; } } public class MyClass { private IMyInterface field = null; public IMyInterface Property { get { return field; } } } public class Program { public static void Method(MyClass @class) { Console.WriteLine(@class.Property.IntfProp.ToString()); } public static void Main(string[] args) { // ************ […]