2011-06-22 17 views
0

如何检索用于初始化RuleViewModelBase的类型T的名称?现在我得到 “RuntimeType”如何检索T型的名称?我总是得到“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; 
     } 
    } 
+0

'typeof(T)'产生'T'的类型。调用'GetType'就可以得到'typeof'返回的值的类型。这显然不是你想要的。 –

回答

6

我想你想:

typeof(T).Name 

相反的:

typeof(T).GetType().Name 

你已经渐渐的类型名称的代码对应于引用您的课程的Type的实例。即实际上您正在做typeof(Type)(或更具体地说,typeof(RuntimeType))并获得该名称。

+0

dohh ... =(。谢谢你的回答。 – burnt1ce