我遇到了我的课程设计中的一个小问题,我遇到了麻烦。在网络搜索无济于事的时候,我希望这里有人会友好地指点我正确的方向。我从理论上公布了整个代码,应该能够在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
如果您可以发布* short *,但将来会提供完整的示例,这将非常有帮助。你给了我们近200行的代码,甚至没有显示哪一行报告了错误。 – 2012-01-06 11:01:03