我的班级设计遇到了一个小问题,我在处理它时遇到了麻烦。搜索网络无济于事我希望有人在这里能够指出我正确的方向。我已经在理论上发布了整个代码,应该能够在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
答案 0 :(得分:1)
我不是百分之百确定这是否是原因 - 但你应该在Person<T>
类上维护通用参数约束。因此,将where T : new()
添加到Person<T>
类。
修改强>
编译代码后,我收到错误:
错误1'T'必须是具有公共无参数构造函数的非抽象类型,才能在泛型类型或方法'Reflection.ObjectFactory'中将其用作参数'T'。SomeClass.cs 67 18 Reflection
通过添加上面提到的where
子句解决了这个错误。在那之后,我没有编译错误。