[英]Intercepting Property Assignment with Attributes
是否可以在屬性上放置一個屬性來攔截對該屬性的賦值調用?
我將如何使用它的一個例子是,當用戶指定null
時,將其上具有此屬性的字符串屬性設置為string.Empty
。
我相信這樣的東西有很多其他可能的用途,但目前這正是我正在尋找的東西。
編輯:
例如:
class A
{
[NotNullableString]
public string SomeString { get; set; }
}
NotNullableStringAttribute
將檢查set
的輸入值,如果為null,則將其替換為string.Empty
。
如果要使用C#屬性執行此操作,則需要使用IL編織。
最受歡迎的圖書館是PostSharp: http ://ayende.com/blog/3640/first-steps-with-post-sharp快遞版是免費的: http : //www.postsharp.net/aspects
如果您堅持在工作流程中沒有構建后的步驟並且您可以使用屬性,那么答案是它目前無法實現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.