本文整理汇总了C#中Catalog.CreateInstance方法的典型用法代码示例。如果您正苦于以下问题:C# Catalog.CreateInstance方法的具体用法?C# Catalog.CreateInstance怎么用?C# Catalog.CreateInstance使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Catalog
的用法示例。
在下文中一共展示了Catalog.CreateInstance方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Catalog_Can_Create_Object_With_Public_Readonly_Field
public void Catalog_Can_Create_Object_With_Public_Readonly_Field()
{
Catalog catalog = new Catalog();
object instance = catalog.CreateInstance<TestClass>();
Assert.IsType<TestClass>(instance);
TestClass cl = instance as TestClass;
Assert.NotEqual(-1, cl.Value);
}
示例2: Catalog_Can_Create_Array
public void Catalog_Can_Create_Array()
{
Catalog catalog = new Catalog();
Type array = typeof(string[]);
object result = catalog.CreateInstance(array);
Assert.IsType(array, result);
}
示例3: Main
static void Main(string[] args)
{
Catalog catalog = new Catalog();
Customer customer = catalog.CreateInstance<Customer>();
Console.WriteLine(JsonConvert.SerializeObject(customer, Formatting.Indented));
Console.ReadKey();
}
示例4: Catalog_Can_Create_Cyclic_Object
public void Catalog_Can_Create_Cyclic_Object()
{
Catalog catalog = new Catalog();
object instance = catalog.CreateInstance<Sample>();
instance.Should().BeOfType<Sample>();
Sample sample = instance as Sample;
Veirfy(sample, 0, catalog.MaxRecursionDepth);
}
示例5: Catalog_Can_Populate_Interface
public void Catalog_Can_Populate_Interface()
{
Catalog catalog = new Catalog();
catalog.RegisterBuilder<TestInterface>()
.WithConstructor(() => new TestClass());
object instance = catalog.CreateInstance(typeof(TestInterface));
instance.Should().NotBeNull();
instance.Should().BeAssignableTo<TestInterface>();
}
示例6: Catalog_Can_Create_Using_Consutrctor
public void Catalog_Can_Create_Using_Consutrctor()
{
Catalog catalog = new Catalog();
object instance = catalog.CreateInstance(typeof(Sample));
Assert.IsType(typeof(Sample), instance);
Sample sample = instance as Sample;
Assert.NotEqual(-1, sample.PrivateInt);
Assert.NotNull(sample.PrivateString);
Assert.NotNull(sample.StringProp);
Assert.NotEqual(DateTime.MinValue, sample.DateProp);
}
示例7: Catalog_Can_Create_Array_Of_Cyclic_Object
public void Catalog_Can_Create_Array_Of_Cyclic_Object()
{
Catalog catalog = new Catalog();
object instance = catalog.CreateInstance<Sample[]>();
instance.Should().BeOfType<Sample[]>();
Sample[] sample = instance as Sample[];
foreach (Sample s in sample)
{
Veirfy(s, 1, catalog.MaxRecursionDepth);
}
}
示例8: Catalog_Can_Create_Class_With_Interface_Property
public void Catalog_Can_Create_Class_With_Interface_Property()
{
Catalog catalog = new Catalog();
catalog.RegisterBuilder<TestInterface>()
.WithConstructor(() => new TestClass());
object instance = catalog.CreateInstance(typeof(ClassWithInterfaceProperty));
instance.Should().BeOfType<ClassWithInterfaceProperty>();
ClassWithInterfaceProperty sample = instance as ClassWithInterfaceProperty;
sample.Property.Should().NotBeNull();
sample.Property.Should().BeAssignableTo<TestInterface>();
}
示例9: Can_Parse_Simple_Class
void Can_Parse_Simple_Class()
{
Catalog catalog = new Catalog();
Sample instance = catalog.CreateInstance<Sample>();
ObjectDataTree dataTree = new ObjectDataTree(instance);
string asString = dataTree.StringValue();
ObjectDataTree otherDataTree = new ObjectDataTree(instance);
string otherString = otherDataTree.StringValue();
Assert.NotEmpty(asString);
Assert.NotEmpty(otherString);
otherString.Should().Be(asString);
}
示例10: Catalog_Can_Create_Class_With_Interface_In_Constructor
public void Catalog_Can_Create_Class_With_Interface_In_Constructor()
{
Catalog catalog = new Catalog();
catalog.RegisterBuilder<TestInterface>()
.WithConstructor(() => new TestClass());
object instance = catalog.CreateInstance(typeof(ClassWithInterfaceInConstructor));
instance.Should().BeOfType<ClassWithInterfaceInConstructor>();
Assert.IsType<ClassWithInterfaceInConstructor>(instance);
ClassWithInterfaceInConstructor sample = instance as ClassWithInterfaceInConstructor;
var property = sample.Get();
property.Should().NotBeNull();
property.Should().BeAssignableTo<TestInterface>();
}
示例11: RunTest
private static void RunTest(Type message)
{
Catalog catalog = new Catalog();
catalog.RegisterBuilder<ObjectPropertyWithCustomValue>().WithPostConstruction(o => ((ObjectPropertyWithCustomValue)o).Value = catalog.CreateInstance<CustomObject>());
catalog.RegisterBuilder<ArrayField>().WithConstructor(() => ArrayField.CreateInstance());
IAssertEquality instance = (IAssertEquality)catalog.CreateInstance(message);
Assert.IsType(message, instance);
ObjectDataTree messageTree = new ObjectDataTree(instance);
using (MemoryStream ms = new MemoryStream())
{
Serialize(new IndisposableStream(ms), instance);
ms.Flush();
ms.Seek(0, SeekOrigin.Begin);
object result = Deserialize(new IndisposableStream(ms), message);
ObjectDataTree resultTree = new ObjectDataTree(result);
resultTree.StringValue().Should().Be(messageTree.StringValue());
instance.AssertEquality(result);
}
}
示例12: Catalog_Can_Create_List
public void Catalog_Can_Create_List()
{
Catalog catalog = new Catalog();
object result = catalog.CreateInstance(typeof(List<string>));
Assert.IsType<List<string>>(result);
}
示例13: Catalog_Can_Create_Object_With_Composed_Collection
public void Catalog_Can_Create_Object_With_Composed_Collection()
{
Catalog catalog = new Catalog();
object result = catalog.CreateInstance(typeof(TestClass));
result.Should().BeOfType<TestClass>();
TestClass instance = result as TestClass;
instance.Value.Should().NotBeNull();
}
示例14: Catalog_Can_Create_Non_Empty_List
public void Catalog_Can_Create_Non_Empty_List()
{
Catalog catalog = new Catalog();
List<List<string>> resultedLists = new List<List<string>>();
for (int i = 0; i < 100; i++) // 100 times since rnd might generate an empty list
{
object result = catalog.CreateInstance(typeof(List<string>));
Assert.IsType<List<string>>(result);
resultedLists.Add(result as List<string>);
}
Assert.True(resultedLists.Any(l => l.Any()));
}