c#按类名和方法名获取自定义属性值

时间:2015-05-18 19:20:13

标签: c#

我创建名为RoleController.cs的类:

[Dynaphore(Id=2)]
    [Dynaphore(Name="Role Module")]
    [Dynaphore(Description="Role will Control Capability of user")]
    public class RoleController : BaseController
    {

        [Dynaphore(Id = 20)]
        [Dynaphore(Name = "List of Role")]
        [Dynaphore(Description = "Show all role in list")]
        public ActionResult Index()
        {
            return View();
        }

        [Dynaphore(Id = 21)]
        [Dynaphore(Name = "Role Editor")]
        [Dynaphore(Description = "Edit Role")]
        public ActionResult Editor(Guid? id)
        {

            var role = dbIntegrated.Roles.Where(p => p.Id == id).FirstOrDefault();
            if (role == null)
            {
                var cons = AccessLogic.GetAllCapabilities();
                List<VMRoleCapabilityItem> model = new List<VMRoleCapabilityItem>();
                foreach (var c in cons)
                {

                    foreach (var a in c.Actions)
                    {
                        VMRoleCapabilityItem domain = new VMRoleCapabilityItem();
                        domain.Controller = c.Controller.Replace("Controller", "");
                        domain.Action = Util.PrettySplitter(a);
                        domain.IsActive = false;
                        VMRoleCapabilityItem result = new VMRoleCapabilityItem();
                        result = domain;
                        model.Add(domain);
                    }

                }
                return View(model);
            }
            else
            {
                var cons = AccessLogic.GetAllCapabilities();
                List<VMRoleCapabilityItem> model = new List<VMRoleCapabilityItem>();
                foreach (var c in cons)
                {

                    foreach (var a in c.Actions)
                    {
                        VMRoleCapabilityItem domain = new VMRoleCapabilityItem();
                        domain.Controller = c.Controller.Replace("Controller", "");
                        domain.Action = Util.PrettySplitter(a);
                        var rc = dbIntegrated.RoleCapabilities
                            .Where(p => p.Controller == c.Controller)
                            .Where(p => p.Action == a)
                            .FirstOrDefault();
                        if (rc != null) domain.IsActive = true;
                        else domain.IsActive = false;
                        model.Add(domain);
                    }

                }
                return View(model);
            }
        }

        [Dynaphore(Id = 22)]
        [Dynaphore(Name = "Save Role")]
        [Dynaphore(Description = "Save a role")]
        public ActionResult Save(VMCapabilityItem domain)
        {

            return RedirectToAction("Index");
        }

        [Dynaphore(Id = 298)]
        [Dynaphore(Name = "Soft Delete Role")]
        [Dynaphore(Description = "Delete Role from view")]
        public ActionResult SoftDelete(Guid id)
        {

            return Json(new { is_success = true });
        }

        [Dynaphore(Id = 299)]
        [Dynaphore(Name = "Hard Delete Role")]
        [Dynaphore(Description = "Delete Role permanently")]
        public ActionResult HardDelete(Guid id)
        {

            return Json(new { is_success = true });
        }

    }

这是我的自定义属性类:

/// <summary>
    /// 
    /// </summary>
    [AttributeUsage(AttributeTargets.Class)]
    public class DynaphoreAttribute : Attribute
    {
        /// <summary>
        /// String field.
        /// </summary>
        int _id;
        String _name;
        String _desc;

        /// <summary>
        /// Attribute constructor.
        /// </summary>
        public DynaphoreAttribute()
        {
        }

        /// <summary>
        /// Get and set.
        /// </summary>
        public int Id
        {
            get { return this._id; }
            set { this._id = value; }
        }

        public String Name
        {
            get { return this._name; }
            set { this._name = value; }
        }

        public String Description {
            get { return this._desc; }
            set { this._desc = value; } 
        }
    }

如何按类名和方法名获取类,名称和描述?

我为它创建了两种方法。 首先得到类ID:

public static int GetDynaphoreClassId(String className)
        {
            return 0;
        }

第二,获取方法ID

public static int GetDynaphoreMethodId(String className, String methodName)
{
    return 0;
}

如何解决这个问题?

0 个答案:

没有答案