本文整理汇总了C#中Dictionary.ToObject方法的典型用法代码示例。如果您正苦于以下问题:C# Dictionary.ToObject方法的具体用法?C# Dictionary.ToObject怎么用?C# Dictionary.ToObject使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Dictionary
的用法示例。
在下文中一共展示了Dictionary.ToObject方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ToObjectEnumPropertyFromString
public void ToObjectEnumPropertyFromString()
{
var dict = new Dictionary<string, object>()
{
{ "EnumProperty", "One" },
};
var value = dict.ToObject<ClassType>();
Assert.Equal(EnumType.One, value.EnumProperty);
}
示例2: ToObjectPrimitiveProperties
public void ToObjectPrimitiveProperties()
{
var dict = new Dictionary<string, object>()
{
{ "StringProperty", "a" },
{ "IntProperty", 1 },
};
var value = dict.ToObject<ClassType>();
Assert.Equal("a", value.StringProperty);
Assert.Equal(1, value.IntProperty);
}
示例3: AsObjectUnknownProperty
public void AsObjectUnknownProperty()
{
var dict = new Dictionary<string, object>()
{
{ "StringProperty", "a" },
{ "IntProperty", 1 },
{ "UnknownProperty", "u" }
};
var value = dict.ToObject<ClassType>();
Assert.Equal("a", value.StringProperty);
Assert.Equal(1, value.IntProperty);
}
示例4: AsObjectPrivateSetter
public void AsObjectPrivateSetter()
{
var dict = new Dictionary<string, object>()
{
{ "StringProperty", "a" },
{ "IntProperty", 1 },
{ "StringPropertyPrivateSetter", "p" }
};
var value = dict.ToObject<ClassType>();
Assert.Equal("a", value.StringProperty);
Assert.Equal(1, value.IntProperty);
Assert.Equal("p", value.StringPropertyPrivateSetter);
}
示例5: AsObjectField
public void AsObjectField()
{
var dict = new Dictionary<string, object>()
{
{ "StringProperty", "a" },
{ "IntProperty", 1 },
{ "StringField", "f" }
};
var value = dict.ToObject<ClassType>();
Assert.Equal("a", value.StringProperty);
Assert.Equal(1, value.IntProperty);
Assert.Equal(null, value.StringField);
}
示例6: DifferentTypes
public void DifferentTypes()
{
// Arrange
var dict = new Dictionary<string, string>()
{
{ "Id", "11" }, // Should be a Guid
{ "Date", "1999-01-01" },
{ "Enum", "One" },
{ "Array", "1,2,3" },
{ "Text", "eleven" },
};
// Act
dict.ToObject<RecursiveData>();
}
示例7: DifferentTypes
public void DifferentTypes()
{
// Arrange
var dict = new Dictionary<string, object>()
{
{ "Id", 11 }, // Should be a Guid
{ "Date", new DateTime(1999, 1, 1) },
{ "Enum", Enumeration.One },
{ "Array", new[] { 1, 2, 3, } },
{ "Text", "eleven" },
};
// Act
dict.ToObject<RecursiveData>();
}
示例8: ToObjectDynamicODataEntry
public void ToObjectDynamicODataEntry()
{
var _ = ODataDynamic.Expression;
var dict = new Dictionary<string, object>()
{
{ "StringProperty", "a" },
{ "IntProperty", 1 },
};
dynamic value = dict.ToObject<ODataEntry>(true);
Assert.Equal("a", value.StringProperty);
Assert.Equal(1, value.IntProperty);
}
示例9: MapsFromDictionaryNonGeneric
public void MapsFromDictionaryNonGeneric()
{
var dictionary = new Dictionary<string, object> { { "PublicProperty", "Public Property" }, { "IgnoreMe", "la la la" } };
var testClass = dictionary.ToObject(typeof(TestClass)) as TestClass;
Assert.AreEqual("Public Property", testClass.PublicProperty);
}
示例10: ToObjectCompoundCollectionProperty
public void ToObjectCompoundCollectionProperty()
{
var dict = new Dictionary<string, object>()
{
{ "StringProperty", "a" },
{ "IntProperty", 1 },
{ "CompoundCollectionProperty", new[]
{
new Dictionary<string, object>() { { "StringProperty", "x" }, { "IntProperty", 1 } },
new Dictionary<string, object>() { { "StringProperty", "y" }, { "IntProperty", 2 } },
new Dictionary<string, object>() { { "StringProperty", "z" }, { "IntProperty", 3 } },
}
}
};
var value = dict.ToObject<ClassType>();
Assert.Equal("a", value.StringProperty);
Assert.Equal(1, value.IntProperty);
for (var index = 0; index < 3; index++)
{
var kv = (dict["CompoundCollectionProperty"] as IList<IDictionary<string, object>>)[index];
Assert.Equal(kv["StringProperty"], value.CompoundCollectionProperty[index].StringProperty);
Assert.Equal(kv["IntProperty"], value.CompoundCollectionProperty[index].IntProperty);
}
}
示例11: ToObjectODataEntry
public void ToObjectODataEntry()
{
var dict = new Dictionary<string, object>()
{
{ "StringProperty", "a" },
{ "IntProperty", 1 },
};
var value = dict.ToObject<ODataEntry>(false);
Assert.Equal("a", value["StringProperty"]);
Assert.Equal(1, value["IntProperty"]);
}
示例12: ToObjectIntCollection
public void ToObjectIntCollection()
{
var dict = new Dictionary<string, object>()
{
{ "StringProperty", "a" },
{ "IntProperty", 1 },
{ "IntCollectionProperty", new [] {1, 2, 3} }
};
var value = dict.ToObject<ClassType>();
Assert.Equal("a", value.StringProperty);
Assert.Equal(1, value.IntProperty);
for (var index = 0; index < 3; index++)
{
Assert.Equal((dict["IntCollectionProperty"] as IList<int>)[index], value.IntCollectionProperty[index]);
}
}
示例13: ToObjectCompoundProperty
public void ToObjectCompoundProperty()
{
var dict = new Dictionary<string, object>()
{
{ "StringProperty", "a" },
{ "IntProperty", 1 },
{ "CompoundProperty", new Dictionary<string, object>() { { "StringProperty", "z" }, { "IntProperty", 0 } } }
};
var value = dict.ToObject<ClassType>();
Assert.Equal("a", value.StringProperty);
Assert.Equal(1, value.IntProperty);
Assert.Equal("z", value.CompoundProperty.StringProperty);
Assert.Equal(0, value.CompoundProperty.IntProperty);
}
示例14: NestedDictionary
public void NestedDictionary()
{
var expected = new RecursiveData
{
Id = new Guid("366f4bd3-6717-4b14-9c79-70515296df7e"),
Date = new DateTime(1999, 1, 1),
Enum = Enumeration.One,
Text = "level 1",
Array = new[] { 1, 2, 3, },
Nested = new RecursiveData
{
Id = new Guid("e591be31-289f-4a99-ba67-288ea24b7d7e"),
Date = new DateTime(1999, 2, 2),
Enum = Enumeration.Two,
Text = "level 2",
Array = new[] { 4, 5, 6, },
Nested = null,
},
};
var dict = new Dictionary<string, object>()
{
// 1st level
{ "Id", expected.Id },
{ "Date", expected.Date },
{ "Enum", expected.Enum },
{ "Text", expected.Text },
{ "Array", expected.Array },
// 2nd level
{
"Nested",
new Dictionary<string, object>
{
{ "Id", expected.Nested.Id },
{ "Date", expected.Nested.Date },
{ "Enum", expected.Nested.Enum },
{ "Text", expected.Nested.Text },
{ "Array", expected.Nested.Array },
{ "Nested", expected.Nested.Nested },
}
}
};
var actual = dict.ToObject<RecursiveData>();
// 1st level
Assert.AreEqual(expected.Id, actual.Id);
Assert.AreEqual(expected.Date, actual.Date);
Assert.AreEqual(expected.Enum, actual.Enum);
Assert.AreEqual(expected.Text, actual.Text);
CollectionAssert.AreEqual(expected.Array, actual.Array);
// 2nd level
Assert.AreEqual(expected.Nested.Id, actual.Nested.Id);
Assert.AreEqual(expected.Nested.Date, actual.Nested.Date);
Assert.AreEqual(expected.Nested.Enum, actual.Nested.Enum);
Assert.AreEqual(expected.Nested.Text, actual.Nested.Text);
CollectionAssert.AreEqual(expected.Nested.Array, actual.Nested.Array);
}
示例15: ToObject_WhenDictionaryContainsDataWhichCannotBeMatched2_MustThrowInvalidOperationException
public void ToObject_WhenDictionaryContainsDataWhichCannotBeMatched2_MustThrowInvalidOperationException()
{
var dictionary = new Dictionary<string, object> { { "Name", "Marbach" }, { "Street", "Elsewhere" }};
Assert.Throws<InvalidOperationException>(() => dictionary.ToObject<Input>());
}