public void DefaultObjectFactorySupportsNonGenericInterfaces() { var sut = new DefaultObjectFactory(); var result = sut.Create(typeof(IList)); Assert.IsAssignableFrom <IList>(result); }
public object Create(Type type) { if (type.IsInterface && defaultInterfaceImplementations.TryGetValue(type, out var implementationType)) { return(Activator.CreateInstance(implementationType)); } return(defaultObjectFactory.Create(type)); }
/// <summary> /// Builds a deserializer that will provide YamlDotNet with the given object instead of creating a new one. /// This will make YamlDotNet update this object when deserializing. /// </summary> /// <param name="objectToBind">The object to set fields on</param> /// <returns>The newly-created deserializer</returns> public static IDeserializer DeserializerUsing(object objectToBind) { IObjectFactory defaultObjectFactory = new DefaultObjectFactory(); Type objectType = objectToBind.GetType(); return(new DeserializerBuilder() .IgnoreUnmatchedProperties() // provide the given object if type matches, fall back to default behavior otherwise. .WithObjectFactory(type => type == objectType ? objectToBind : defaultObjectFactory.Create(type)) .Build()); }
public object Create(Type type) { if (type == typeof(DNA)) { dna = new DNA(); return(dna); } if (type == typeof(Entity)) { return(new Entity { DNA = dna }); } return(fallback.Create(type)); }