2012-01-06 87 views
2

我遇到了我的课程设计中的一个小问题,我遇到了麻烦。在网络搜索无济于事的时候,我希望这里有人会友好地指点我正确的方向。我从理论上公布了整个代码,应该能够在VS中做一个简单的复制和粘贴,并将错误可视化。通用类型所需的帮助 - 类型不能用作类型参数

在此先感谢,Onam。

完整错误: 类型'投诉人'不能用作泛型类型或方法'Person'中的类型参数'T'。没有从“投诉人”到“ObjectFactory”的隐式引用转换。

#region : Complainant : 
public class Complainant : Person<Complainant> 
{ 
    #region Properties... 

    #region ComplainantId 
    public Int64 ComplainantId { get; private set; } 
    #endregion 

    #region IsApproachable 
    public bool IsApproachable { get; set; } 
    #endregion 

    #endregion 

    #region Constructors... 
    public Complainant() 
    { 
    } 
    #endregion 

    #region Methods... 

    #region Load 
    public void Load(Int64 objectId) 
    { 
     base.Hydrate(objectId); 
    } 
    #endregion 

    #endregion 
} 
#endregion 

#region : Person : 
public class Person<T> : ObjectFactory<T> 
{ 
    #region Properties... 

    #region PersonId 
    public Int64 PersonId { get; protected set; } 
    #endregion 

    #region DOB 
    public DateTime DOB { get; set; } 
    #endregion 

    #region Email 
    public string Email { get; set; } 
    #endregion 

    #region Forename 
    public string Forename { get; set; } 
    #endregion 

    #region Fullname 
    public string Fullname { get { return Forename + " " + Surname; } } 
    #endregion 

    #region HomeTel 
    public string HomeTel { get; set; } 
    #endregion 

    #region Initials 
    public string Initials { get; set; } 
    #endregion 

    #region Mobile 
    public string Mobile { get; set; } 
    #endregion 

    #region Surname 
    public string Surname { get; set; } 
    #endregion 

    #region WorkTel 
    public string WorkTel { get; set; } 
    #endregion 

    #endregion 

    #region Constructor 
    public Person() 
    { 
    } 
    #endregion 
} 
#endregion 

#region : Property : 
public class Property 
{ 
    #region Properties... 

    #region Inherits 
    [XmlAttribute("Inherits")] 
    public string Inherits { get; set; } 
    #endregion 

    #region IsPrimaryKey 
    [XmlAttribute("IsPrimaryKey")] 
    public bool IsPrimaryKey { get; set; } 
    #endregion 

    #region Name 
    [XmlAttribute("Name")] 
    public string Name { get; set; } 
    #endregion 

    #endregion 

    #region Constructors... 

    public Property(string name, bool isPrimaryKey) 
    { 
     this.Name = name; 
     this.IsPrimaryKey = isPrimaryKey; 
    } 

    public Property() 
    { 
    } 

    #endregion 

    #region Methods 


    #endregion 
} 
#endregion 

#region : ObjectFactory : 
public class ObjectFactory<T> where T : new() 
{ 
    #region Members... 
    static Expression<Func<T>> __x =() => new T(); 
    static Func<T> __function = __x.Compile(); 
    static Dictionary<Type, List<Property>> __cache = new Dictionary<Type, List<Property>>(); 
    #endregion 

    #region Constructors 
    public ObjectFactory() 
    { 
     Type _type = typeof(T); 

     if (!__cache.ContainsKey(_type)) 
     { 
      __cache.Add(_type, Deserialize(_type.Name)); 
     } 
    } 
    #endregion 

    #region Methods 

    #region Build 
    public static T Build() 
    { 
     return __function(); 
    } 
    #endregion 

    #region Deserialize 
    private List<Property> Deserialize(string objectName) 
    { 
     XmlSerializer _serializer = new XmlSerializer(typeof(List<Property>)); 
     using (Stream _stream = File.OpenRead(String.Format(@"C:\_XMLFiles\{0}.xml", objectName))) 
     { 
      return (List<Property>)_serializer.Deserialize(_stream); 
     } 
    } 
    #endregion 

    #region Hydrate 
    protected void Hydrate(Int64 objectId) 
    { 
     List<Property> _properties = new List<Property>(); 

     if (__cache.TryGetValue(typeof(T), out _properties)) 
     { 
      object[] o = typeof(T).GetProperties(); 
      foreach (PropertyInfo pi in typeof(T).GetProperties()) 
      { 
       string s = pi.Name; 
       //if (pi.Name == name) 
       //{ 
        //pi.SetValue(this, value, null); 
       //} 
      } 
     } 
    } 
    #endregion 

    #endregion 
} 
#endregion 
+6

如果您可以发布* short *,但将来会提供完整的示例,这将非常有帮助。你给了我们近200行的代码,甚至没有显示哪一行报告了错误。 – 2012-01-06 11:01:03

回答

1

我不是100%肯定,如果这是什么原因 - 但你应该保持对Person<T>类泛型参数的约束。因此请将where T : new()添加到Person<T>类。

编辑

编译代码后,我得到的错误:

Error 1 'T' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method 'Reflection.ObjectFactory' SomeClass.cs 67 18 Reflection

此错误是由加入我上面提到的where条款解决。在那之后,我没有收到编译错误。

+0

啊对了,所以不能让我的类Person从ObjectFactory继承?我希望它的原因是因为它缓存了任何实例化类型(我正在使用通用方法来保存,删除和加载对象)并执行其他操作。我希望它继承的主要原因是因为ObjectFactory类比使用Activator.CreateInstance <>更快地创建其他类的实例。 – 2012-01-06 11:19:43

+0

你的Person类可以从ObjectFactory继承,但是因为它将通用参数类型传递给ObjectFactory,它必须具有匹配约束。只要添加'new()'约束就可以解决问题(就我所知)。 – 2012-01-06 11:26:26

+0

这是完美的!非常感谢,我现在可以继续进行我的小方案,再次感谢:) – 2012-01-06 13:22:31

相关问题