C# 仅将扩展方法限制为目标EF实体
我制作了一个扩展方法,用于从EF实体生成可序列化的字典:C# 仅将扩展方法限制为目标EF实体,c#,asp.net,entity-framework,C#,Asp.net,Entity Framework,我制作了一个扩展方法,用于从EF实体生成可序列化的字典: public static class Extensions { public static IDictionary<string, object> ToSerializable(this object obj) { var result = new Dictionary<string, object>(); foreach (var property in obj
public static class Extensions
{
public static IDictionary<string, object> ToSerializable(this object obj)
{
var result = new Dictionary<string, object>();
foreach (var property in obj.GetType().GetProperties().ToList())
{
var value = property.GetValue(obj, null);
if (value != null && (value.GetType().IsPrimitive
|| value is decimal || value is string || value is DateTime
|| value is List<object>))
{
result.Add(property.Name, value);
}
}
return result;
}
}
我想知道是否有任何方法可以限制它仅在我的实体上可用,而不是在所有对象上可用:s
会把范围缩小一点。如果需要更多,则需要为所有实体指定标记接口,并使用:
public static IDictionary<string, T> ToSerializable(this T obj) where T:IEntity
会把范围缩小一点。如果需要更多,则需要为所有实体指定标记接口,并使用:
public static IDictionary<string, T> ToSerializable(this T obj) where T:IEntity
Patryk回答的代码:
public interface ISerializableEntity { };
public class CustomerEntity : ISerializableEntity
{
....
}
public static class Extensions
{
public static IDictionary<string, object> ToSerializable(
this ISerializableEntity obj)
{
var result = new Dictionary<string, object>();
foreach (var property in obj.GetType().GetProperties().ToList())
{
var value = property.GetValue(obj, null);
if (value != null && (value.GetType().IsPrimitive
|| value is decimal || value is string || value is DateTime
|| value is List<object>))
{
result.Add(property.Name, value);
}
}
return result;
}
}
查看此代码如何与marker接口一起工作,您可以选择将序列化方法放在接口中,以避免反射,并更好地控制序列化的内容以及如何对其进行编码或加密:
public interface ISerializableEntity
{
Dictionary<string, object> ToDictionary();
};
public class CustomerEntity : ISerializableEntity
{
public string CustomerName { get; set; }
public string CustomerPrivateData { get; set; }
public object DoNotSerializeCustomerData { get; set; }
Dictionary<string, object> ISerializableEntity.ToDictionary()
{
var result = new Dictionary<string, object>();
result.Add("CustomerName", CustomerName);
var encryptedPrivateData = // Encrypt the string data here
result.Add("EncryptedCustomerPrivateData", encryptedPrivateData);
}
return result;
}
Patryk回答的代码:
public interface ISerializableEntity { };
public class CustomerEntity : ISerializableEntity
{
....
}
public static class Extensions
{
public static IDictionary<string, object> ToSerializable(
this ISerializableEntity obj)
{
var result = new Dictionary<string, object>();
foreach (var property in obj.GetType().GetProperties().ToList())
{
var value = property.GetValue(obj, null);
if (value != null && (value.GetType().IsPrimitive
|| value is decimal || value is string || value is DateTime
|| value is List<object>))
{
result.Add(property.Name, value);
}
}
return result;
}
}
查看此代码如何与marker接口一起工作,您可以选择将序列化方法放在接口中,以避免反射,并更好地控制序列化的内容以及如何对其进行编码或加密:
public interface ISerializableEntity
{
Dictionary<string, object> ToDictionary();
};
public class CustomerEntity : ISerializableEntity
{
public string CustomerName { get; set; }
public string CustomerPrivateData { get; set; }
public object DoNotSerializeCustomerData { get; set; }
Dictionary<string, object> ISerializableEntity.ToDictionary()
{
var result = new Dictionary<string, object>();
result.Add("CustomerName", CustomerName);
var encryptedPrivateData = // Encrypt the string data here
result.Add("EncryptedCustomerPrivateData", encryptedPrivateData);
}
return result;
}
你能使用一个公共基类,甚至是空的,并从中继承吗?这需要一点手工工作,但你可以声明一个标记接口,例如IEntity,让每个实体实现该接口,然后将扩展方法附加到IEntity。你能使用一个公共基类,甚至是空的,它需要一些手工工作,但您可以声明一个标记接口,例如IEntity,使每个实体实现该接口,然后将扩展方法附加到IEntity