本文整理汇总了C#中MappingSchema.SetConverter方法的典型用法代码示例。如果您正苦于以下问题:C# MappingSchema.SetConverter方法的具体用法?C# MappingSchema.SetConverter怎么用?C# MappingSchema.SetConverter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MappingSchema
的用法示例。
在下文中一共展示了MappingSchema.SetConverter方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestEnumString
public void TestEnumString(string context, Action<MappingSchema> initMappingSchema)
{
var ms = new MappingSchema();
ms.SetConverter<Dictionary<string, string>, string> (obj => obj == null ? null : obj.Keys.FirstOrDefault());
ms.SetConverter<Dictionary<string, string>, DataParameter>(obj => obj == null ? null : new DataParameter { Value = obj.Keys.FirstOrDefault(), DataType = DataType.NVarChar });
ms.SetConverter<string, Dictionary<string, string>> (txt => txt == null ? null : new Dictionary<string, string> { { txt, txt } });
initMappingSchema(ms);
using (var db = GetDataContext(context, ms))
{
var id = Convert.ToInt32(db.InsertWithIdentity(new Person2
{
FirstName = new Dictionary<string, string> { { "123", "123" } },
LastName = "456",
MiddleName = "789",
Gender = Gender.M
}));
var p = db.GetTable<PurePerson>().First(t => t.PersonID == id);
Assert.AreEqual(Gender.M.ToString(), p.Gender);
Assert.AreEqual("123", p.FirstName);
db.Delete(p);
}
}
示例2: BaseSchema2
public void BaseSchema2()
{
var ms1 = new MappingSchema();
var ms2 = new MappingSchema(ms1);
Convert<DateTime,string>.Lambda = d => d.ToString(DateTimeFormatInfo.InvariantInfo);
ms1.SetConverter<DateTime,string>(d => d.ToString(new CultureInfo("en-US", false).DateTimeFormat));
ms2.SetConverter<DateTime,string>(d => d.ToString(new CultureInfo("ru-RU", false).DateTimeFormat));
{
var c0 = Convert<DateTime,string>.Lambda;
var c1 = ms1.GetConverter<DateTime,string>();
var c2 = ms2.GetConverter<DateTime,string>();
Assert.AreEqual("01/20/2012 16:30:40", c0(new DateTime(2012, 1, 20, 16, 30, 40, 50, DateTimeKind.Utc)));
Assert.AreEqual("1/20/2012 4:30:40 PM", c1(new DateTime(2012, 1, 20, 16, 30, 40, 50, DateTimeKind.Utc)));
Assert.AreEqual("20.01.2012 16:30:40", c2(new DateTime(2012, 1, 20, 16, 30, 40, 50, DateTimeKind.Utc)));
}
Convert<string,DateTime>.Expression = s => DateTime.Parse(s, DateTimeFormatInfo.InvariantInfo);
ms1.SetConvertExpression<string,DateTime>(s => DateTime.Parse(s, new CultureInfo("en-US", false).DateTimeFormat));
ms2.SetConvertExpression<string,DateTime>(s => DateTime.Parse(s, new CultureInfo("ru-RU", false).DateTimeFormat));
{
var c0 = Convert<string,DateTime>.Lambda;
var c1 = ms1.GetConverter<string,DateTime>();
var c2 = ms2.GetConverter<string,DateTime>();
Assert.AreEqual(new DateTime(2012, 1, 20, 16, 30, 40), c0("01/20/2012 16:30:40"));
Assert.AreEqual(new DateTime(2012, 1, 20, 16, 30, 40), c1("1/20/2012 4:30:40 PM"));
Assert.AreEqual(new DateTime(2012, 1, 20, 16, 30, 40), c2("20.01.2012 16:30:40"));
}
}