开发者

How to retrieve the name of type T? I'm always getting "RuntimeType"

How do I retrieve the name of the type T that was used to initialize RuleViewModelBase? Right now I'm ge开发者_如何学编程tting "RuntimeType"

public abstract class RuleViewModelBase<T> : IRuleViewModel where T : RuleEntity, new()
    {
        public bool Editable
        {
            set
            {
                _editable = value;
                if (Condition != null)
                {
                    Condition.Editable = value;
                }
                if (Content != null)
                {
                    Content.Editable = value;
                }
            }
            get
            {
                return _editable;
            }
        }
        private bool _editable;
        private string _groupsJson;
        private List<RuleGroupJM> _groups;
        public string RuleType { get { 
            return typeof(T).GetType().Name; } 
        }
        public RuleContentVm Content { set; get; }
        public RuleConditionVm Condition { set; get; }
        public virtual void SaveToEntity(T rule)
        {

        }
        public RuleEntity ConvertToEntity()
        {
            T rule = new T();
            rule = (T)this.Content.SaveToEntity(rule);
            rule = (T)this.Condition.SaveToEntity(rule);
            SaveToEntity(rule);
            return rule;
        }
    }


I think you want:

typeof(T).Name  

Instead of:

typeof(T).GetType().Name

The code you had was getting the name of the type that corresponds to the instance of Type that refers to your class. i.e. effectively you were doing typeof(Type) (or more specifically, typeof(RuntimeType)) and getting the name of that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜