我知道属性非常有用。有一些预定义的属性,例如[Browsable(false)],它允许您在财产选项卡中隐藏财产。下面是一个解释属性的好问题:.NET中的属性是什么?
您在项目中实际使用的预定义属性(及其名称空间)是什么?
我知道属性非常有用。有一些预定义的属性,例如[Browsable(false)],它允许您在财产选项卡中隐藏财产。下面是一个解释属性的好问题:.NET中的属性是什么?
您在项目中实际使用的预定义属性(及其名称空间)是什么?
当前回答
在我们当前的项目中,我们使用
[ComVisible(false)]
它控制单个托管类型或成员或程序集中所有类型对COM的可访问性。
更多信息
其他回答
在霍夫斯塔德精神中,[Attribute]属性非常有用,因为它是您创建自己属性的方式。我使用了属性而不是接口来实现插件系统,为Enum添加描述,模拟多重调度和其他技巧。
如果项目不在您的解决方案中,[EditorBrowsable(EditorBrowsableState.Never)]允许您对IntelliSense隐藏财产和方法。非常有助于隐藏流畅接口的无效流。您希望多长时间使用GetHashCode()或Equals()?
对于MVC,[ActionName(“Name”)]允许您使用相同的方法签名进行Get操作和Post操作,或者在操作名称中使用破折号,否则,如果不为其创建路由,就无法实现这一点。
我喜欢System.Diagnostics中的[DuggerStepThrough]。
它非常方便,可以避免使用那些单行do-nothing方法或财产(如果您被迫在早期的.Net中工作,而没有自动财产)。将属性放在一个短方法或属性的getter或setter上,即使在调试器中单击“stepinto”,您也会立即执行。
我通过CodeSmith生成数据实体类,并为一些验证例程使用属性。下面是一个示例:
/// <summary>
/// Firm ID
/// </summary>
[ChineseDescription("送样单位编号")]
[ValidRequired()]
public string FirmGUID
{
get { return _firmGUID; }
set { _firmGUID = value; }
}
我得到了一个实用程序类,根据附加到数据实体类的属性进行验证。代码如下:
namespace Reform.Water.Business.Common
{
/// <summary>
/// Validation Utility
/// </summary>
public static class ValidationUtility
{
/// <summary>
/// Data entity validation
/// </summary>
/// <param name="data">Data entity object</param>
/// <returns>return true if the object is valid, otherwise return false</returns>
public static bool Validate(object data)
{
bool result = true;
PropertyInfo[] properties = data.GetType().GetProperties();
foreach (PropertyInfo p in properties)
{
//Length validatioin
Attribute attribute = Attribute.GetCustomAttribute(p,typeof(ValidLengthAttribute), false);
if (attribute != null)
{
ValidLengthAttribute validLengthAttribute = attribute as ValidLengthAttribute;
if (validLengthAttribute != null)
{
int maxLength = validLengthAttribute.MaxLength;
int minLength = validLengthAttribute.MinLength;
string stringValue = p.GetValue(data, null).ToString();
if (stringValue.Length < minLength || stringValue.Length > maxLength)
{
return false;
}
}
}
//Range validation
attribute = Attribute.GetCustomAttribute(p,typeof(ValidRangeAttribute), false);
if (attribute != null)
{
ValidRangeAttribute validRangeAttribute = attribute as ValidRangeAttribute;
if (validRangeAttribute != null)
{
decimal maxValue = decimal.MaxValue;
decimal minValue = decimal.MinValue;
decimal.TryParse(validRangeAttribute.MaxValueString, out maxValue);
decimal.TryParse(validRangeAttribute.MinValueString, out minValue);
decimal decimalValue = 0;
decimal.TryParse(p.GetValue(data, null).ToString(), out decimalValue);
if (decimalValue < minValue || decimalValue > maxValue)
{
return false;
}
}
}
//Regex validation
attribute = Attribute.GetCustomAttribute(p,typeof(ValidRegExAttribute), false);
if (attribute != null)
{
ValidRegExAttribute validRegExAttribute = attribute as ValidRegExAttribute;
if (validRegExAttribute != null)
{
string objectStringValue = p.GetValue(data, null).ToString();
string regExString = validRegExAttribute.RegExString;
Regex regEx = new Regex(regExString);
if (regEx.Match(objectStringValue) == null)
{
return false;
}
}
}
//Required field validation
attribute = Attribute.GetCustomAttribute(p,typeof(ValidRequiredAttribute), false);
if (attribute != null)
{
ValidRequiredAttribute validRequiredAttribute = attribute as ValidRequiredAttribute;
if (validRequiredAttribute != null)
{
object requiredPropertyValue = p.GetValue(data, null);
if (requiredPropertyValue == null || string.IsNullOrEmpty(requiredPropertyValue.ToString()))
{
return false;
}
}
}
}
return result;
}
}
}
我的投票将是有条件的
[Conditional("DEBUG")]
public void DebugOnlyFunction()
{
// your code here
}
您可以使用它添加具有高级调试功能的函数;与Debug.Write类似,它只在调试构建中调用,因此允许您将复杂的调试逻辑封装在程序的主流之外。