注意:我知道使用动态linq创建它很简单,但我想学习.
我想创建一个"找到"的lambda:Name = David AND Age = 10.
class Person
{
public int Age { get; set; }
public string Name { get; set; }
}
var lambda = LabmdaExpression<Person>("Name", "David", "Age", 10);
static Expression<Func<T, bool>> LabmdaExpression<T>(string property1, string value1, string property2, int value2)
{
ParameterExpression parameterExpression = Expression.Parameter(typeof(Person), "o");
MemberExpression memberExpression1 = Expression.PropertyOrField(parameterExpression, property1);
MemberExpression memberExpression2 = Expression.PropertyOrField(parameterExpression, property2);
ConstantExpression valueExpression1 = Expression.Constant(value1, typeof(string));
ConstantExpression valueExpression2 = Expression.Constant(value2, typeof(int));
BinaryExpression binaryExpression1 = Expression.Equal(memberExpression1, valueExpression1);
BinaryExpression binaryExpression2 = Expression.Equal(memberExpression2, valueExpression2); …Run Code Online (Sandbox Code Playgroud) 当我尝试序列化此集合时,name属性未序列化.
public class BCollection<T> : List<T> where T : B_Button
{
public string Name { get; set; }
}
BCollection<BB_Button> bc = new BCollection<B_Button>();
bc.Name = "Name";// Not Serialized!
bc.Add(new BB_Button { ID = "id1", Text = "sometext" });
JavaScriptSerializer serializer = new JavaScriptSerializer();
string json = serializer.Serialize(bc);
Run Code Online (Sandbox Code Playgroud)
只有当我创建一个新类(没有List<t>继承),并定义字符串Name属性和List<B_Button> bc = new List<B_Button>();属性时,我才能得到正确的结果.
无法理解我做错了什么,结果集是空的.
我的代码:
class Class1
{
public static object DeSerialize()
{
object resultObject;
XmlSerializer serializer = new XmlSerializer(typeof(PointsContainer));
using (TextReader textReader = new StreamReader(@"d:\point.xml"))
{
resultObject = serializer.Deserialize(textReader);
}
return resultObject;
}
}
[Serializable]
[XmlRoot("Points")]
public class PointsContainer
{
[XmlElement("Point")]
private List<Point> items = new List<Point>();
public List<Point> Items
{
get { return items; }
set { items = value; }
}
}
[Serializable]
public class Point
{
[XmlAttribute]
public bool x { get; set; }
[XmlAttribute]
public bool y { …Run Code Online (Sandbox Code Playgroud)