任何人都可以告訴我有關方法的自定義屬性。我需要傳遞一個字符串給屬性。如果字符串爲true,那麼我將訪問方法,否則不訪問方法。通過自定義屬性爲方法訪問傳遞值
回答
我不確定,如果我錯誤地理解了你的問題。但是你是否在談論下面那種裝飾方法的屬性。我在探索屬性時創建了這些代碼。我在這裏粘貼它。希望能幫助到你。
在此,我創建了屬性[Allow(「Valid」)],如果它有效,我們可以調用該方法,否則不會。
namespace ConsoleApplication1
{
using System;
[AttributeUsage(AttributeTargets.All)]
public class AllowAttribute : System.Attribute
{
public readonly string SomeString;
public AllowAttribute(string someString) // your string is passed in custom attribute
{
this.SomeString = someString;
}
}
public interface IAllowAttributeInvoker
{
object AllowAttributeInvokeMethod<T>(string methodName, T classInstance, object[] parametersArray);
}
public class AllowAttributeInvoker: IAllowAttributeInvoker
{
public object AllowAttributeInvokeMethod<T>(string methodName, T classInstance, object[] parametersArray)
{
System.Reflection.MemberInfo info = typeof(T).GetMethod(methodName);
if (IsAttributeValid(info))
{
var method = (typeof (T)).GetMethod(methodName);
Console.WriteLine("Invoking method");
var result = method.Invoke(classInstance, parametersArray);
return result;
}
else
{
Console.WriteLine("Can not invoke this method.");
}
return null;
}
private static bool IsAttributeValid(MemberInfo member)
{
foreach (object attribute in member.GetCustomAttributes(true))
{
if (attribute is AllowAttribute && ((AllowAttribute)attribute).SomeString == "Valid")
{
return true;
}
}
return false;
}
}
public class EmployeeService :AllowAttributeInvoker
{
public object PaySalary()
{
return AllowAttributeInvokeMethod("PaySalaryInvoke", this, null);
}
[Allow("Valid")]
public void PaySalaryInvoke()
{
Console.WriteLine("Salary Paid.");
}
}
class Program
{
static void Main(string[] args)
{
Console.ReadLine();
EmployeeService service = new EmployeeService();
service.PaySalary();
Console.ReadLine();
}
}
}
1,您可以定義公共用戶訪問列表:在構造函數中
public List<string> AccessRules = new List<string>();
2,設置用戶訪問規則:
AccessRules.AddRange(new[] { "GetCurrentDateTime", "GetCurrentDate" });
3,在安全的方法檢查用戶訪問規則
public DateTime GetCurrentDateTime()
{
bool haveAccess = AccessRules.Any(c => c == "GetCurrentDateTime");
if (haveAccess)
{
return DateTime.Now;
}
return null;
}
謝謝Hamix – Elamparthi 2014-09-06 11:05:58
喜的朋友, 我需要在C#中做自定義屬性的幫助。 我有3個字符串變量...名稱爲A,B,C。 [自定義屬性,如果(A = true)] //它在調用方法時進入方法。 [自定義屬性,如果(B = false)] //它終止 [自定義屬性,如果(C = true)] //與A相同則向方法中輸入int。 public _somemethod() { //部分代碼 } – Elamparthi 2014-09-08 05:17:28
- 1. 通過jquery訪問自定義屬性
- 2. VueJS通過屬性值傳遞的自定義屬性的子組件
- 3. 無法通過javascript訪問自定義文本框屬性
- 4. 訪問自定義選擇屬性值
- 5. 訪問自定義屬性的值
- 6. 訪問自定義屬性
- 7. 訪問自定義屬性
- 8. 訪問自定義屬性
- 9. Nativescript自定義屬性無法訪問
- 10. 無法訪問自定義屬性
- 11. 如何通過lxml.sax接口傳遞自定義屬性?
- 12. 的WebForms傳遞的ClientID通過自定義屬性
- 13. 通過MKAnnotation傳遞自定義屬性CalloutAccessoryControlTapped
- 14. 通過自定義屬性設置特定屬性的值
- 15. 通過自定義屬性訪問元素
- 16. TinyMCE編輯器 - 如何通過jQuery訪問自定義屬性
- 17. 如何通過名稱訪問自定義組件屬性?
- 18. 通過自定義控件的屬性定義傳遞渲染功能
- 19. 如何通過屬性名稱訪問自定義集合屬性
- 20. 通過自定義protocoll通過.reg文件傳遞的訪問參數
- 21. 傳遞參數和自定義屬性
- 22. Tinymce自定義標籤傳遞屬性
- 23. Laravel的getAttributes()方法忽略自定義屬性訪問器
- 24. 訪問自定義屬性的更簡單方法?
- 25. 通過委託方法攜帶自定義屬性
- 26. C#的主要方法,通過自定義屬性裝飾
- 27. 通過自定義控件屬性或自定義控件的自定義事件傳遞函數?
- 28. 訪問屬性通過傳遞拉姆達
- 29. 無法通過parcelable傳遞自定義類 - 爲什麼?
- 30. 通過父屬性訪問值
非常感謝您的寶貴意見Ganesh – Elamparthi 2014-09-06 17:25:40
歡迎您。如果回答有幫助,請投票,這對社區更好。 – Ganesh 2014-09-07 05:28:53
Ganesh可以告訴我xml屬性中的同樣的東西...只需xml屬性讀取該方法。如果該方法與給定字符串相比時爲真,則它允許該方法,否則不會。 – Elamparthi 2014-09-07 07:28:59