gpt4 book ai didi

c# - 泛型类型需要帮助 - 类型不能用作类型参数

转载 作者:太空宇宙 更新时间:2023-11-03 14:08:06 24 4
gpt4 key购买 nike

我在类设计中遇到了一个小问题,我很难处理它。在网上搜索无济于事后,我希望这里的人会友善地指出我正确的方向。我已经发布了整个代码,所以理论上应该能够在 VS 中进行简单的复制和粘贴并可视化错误。

提前致谢,Onam。

完整错误:类型“Complainant”不能用作泛型类型或方法“Person”中的类型参数“T”。没有从“Complainant”到“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

最佳答案

我不是 100% 确定这是否是原因 - 但您应该在 Person<T> 上维护通用参数约束类(class)。所以添加 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 解决。我上面提到的条款。在那之后,我没有得到任何编译错误。

关于c# - 泛型类型需要帮助 - 类型不能用作类型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8756800/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com