コード例 #1
0
ファイル: ObjectConverterFactory.cs プロジェクト: bash/nageli
        public ITomlConverter CreateConverter(Type typeToConvert, ITomlSerializerContext context)
        {
            var typeToCreate        = context.GetDefaultImplementation(typeToConvert) ?? typeToConvert;
            var constructor         = GetConstructorForDeserialization(typeToCreate);
            var parameterConverters = constructor.GetParameters().Select(p => GetCachedParameterInfo(p, context));

            return((ITomlConverter)Activator.CreateInstance(
                       typeof(ObjectConverter <>).MakeGenericType(typeToConvert),
                       constructor,
                       parameterConverters.ToImmutableList()) !);
        }
コード例 #2
0
        public ITomlConverter CreateConverter(Type typeToConvert, ITomlSerializerContext context)
        {
            var itemType = IsIEnumerable(typeToConvert)
                ? typeToConvert.GetGenericArguments()[0]
                : typeToConvert.GetInterfaces().Single(IsIEnumerable).GetGenericArguments()[0];

            var typeToCreate = context.GetDefaultImplementation(typeToConvert) ?? typeToConvert;

            var factory = CollectionCreatorFactories.FirstOrDefault(f => f.CanCreate(typeToCreate, itemType))
                          ?? throw new TomlException($"No creator found for collection {typeToCreate}");

            return((ITomlConverter)Activator.CreateInstance(
                       typeof(CollectionConverter <,>).MakeGenericType(typeToConvert, itemType),
                       factory.CreateCollectionCreator(typeToCreate, itemType),
                       context) !);
        }