Tag: 属性

是否可以将属性添加到分部类中的属性?

我不认为这是可能的,但由于我没有明确的MSDN清晰度,我觉得最好问一下。 假设我们有一个类如下。 public partial class Hazaa { public int Shazoo { get; set; } } 然后,我想将Shazoo归为SuperCool, 但我希望在另一个文件中这样做。 由于我使用的是部分类,我可以按如下方式添加新属性。 public partial class Hazaa { [SuperCool] public int Wheee { get; set; } } 但是,我可以通过在后者中编写代码来声明第一个样本中声明的属性吗? 我怀疑这是可能的,但我很乐意经过纠正。 如果是这样,语法是什么?

如果属性访问,C#Struct方法不会保存值

我需要创建一个看起来像int的结构(但是我需要一个额外的字段……),所以我创建了一个名为TestStruct的新结构,添加了一个我需要的方法(test())并重载了一些运算符,它看起来运作良好…… 下面的示例显示了问题。 如果从Val属性执行结构test()方法,则Val属性似乎丢失了值,但是如果在Val2变量上执行该方法,则该值似乎保持正确的值… 为什么会这样? static class Program { /// /// The main entry point for the application. /// [STAThread] static void Main() { new TestClass(); } } public class TestClass { public TestStruct Val { get; set; } private TestStruct Val2; public TestClass() { Val.test(); Console.WriteLine(Val + “-> why is it not 10?”); //Direct assignment […]

如果多个成员具有相同的属性,如何抛出编译器错误

简单的问题,如何强制C#编译器抛出编译错误。 更新:也许最好使用Assert.Fail()代替? 我有一个自定义属性,只应该应用于一个类的一个成员。 在我的另一个类的静态方法中,如果有多个成员应用了该属性,它会查找该成员并希望它失败(不抛出exception)。 public class Foo { [MyCustomAttribute] public String FooString { get; set; } [MyCustomAttribute] public String OtherFooString { get; set; } } public class Bar where T : class, new() { static Bar() { //If more than one member of type Foo has MyCustomAttribute //applied to it compile error or Assert.Fail()? } […]

如何在C#中访问usercontrol的属性

我用一个文本框和一个richtextbox创建了一个C#usercontrol。 如何从usercontrol外部访问richtextbox的属性。 例如..如果我把它放在一个表单中,我怎么能使用richtextbox的文本属性??? 谢谢

在运行时添加属性

我有一个程序员可以用来动态添加新属性的类。 为此,它实现了ICustomTypeDescriptor ,以便能够覆盖GetProperties()方法。 public class DynamicProperty { public object Value { get; set; } public Type Type { get; set; } public string Name { get; set; } public Collection Attributes { get; set; } } public class DynamicClass : ICustomTypeDescriptor { // Collection to code add dynamic properties public KeyedCollection Properties { get; private […]

C#自动实现的静态属性是否是线程安全的?

我想知道C#是否自动实现了属性,比如public static T Prop { get; set; } public static T Prop { get; set; } ,是否是线程安全的。 谢谢!

‘this’关键字作为属性

我很了解C#,但这对我来说很奇怪。 在一些旧程序中,我看到了这段代码: public MyType this[string name] { ……some code that finally return instance of MyType } 怎么称呼? 有什么用?

有人知道快速获取枚举值的自定义属性吗?

这可能是最好的例子。 我有一个属性的枚举: public enum MyEnum { [CustomInfo(“This is a custom attrib”)] None = 0, [CustomInfo(“This is another attrib”)] ValueA, [CustomInfo(“This has an extra flag”, AllowSomething = true)] ValueB, } 我想从实例中获取这些属性: public CustomInfoAttribute GetInfo( MyEnum enumInput ) { Type typeOfEnum = enumInput.GetType(); //this will be typeof( MyEnum ) //here is the problem, GetField takes a string […]

使用公共字段的最佳做法是什么?

当我写一个类时,我总是通过这样的公共属性公开私有字段: private int _MyField; public int MyField { get{return _MyField; } 什么时候可以公开这样的公共字段: public int MyField; 我正在创建一个名为Result的结构,我的意图是这样做: public Result(bool result, string message) { Result = result; Message = message; } public readonly int Result; public readonly int Message; 什么是最佳做法? 这样做好吗?

Attribute.IsDefined没有看到使用MetadataType类应用的属性

如果我通过MetadataType属性将属性应用于部分类,则不会通过Attribute.IsDefined()找到这些属性。 任何人都知道为什么,或者我做错了什么? 下面是我为此创建的测试项目,但我真的尝试将自定义属性应用于LINQ to SQL实体类 – 就像这个问题中的答案一样。 谢谢! using System; using System.ComponentModel.DataAnnotations; using System.Reflection; namespace MetaDataTest { class Program { static void Main(string[] args) { PropertyInfo[] properties = typeof(MyTestClass).GetProperties(); foreach (PropertyInfo propertyInfo in properties) { Console.WriteLine(Attribute.IsDefined(propertyInfo, typeof(MyAttribute))); Console.WriteLine(propertyInfo.IsDefined(typeof(MyAttribute), true)); Console.WriteLine(propertyInfo.GetCustomAttributes(true).Length); // Displays: // False // False // 0 } Console.ReadLine(); } } [MetadataType(typeof(MyMeta))] public partial […]