コード例 #1
0
        public object Create(Type targetType, object source)
        {
            StreamFactory.Refresh();
            if (source != null && Resources.Contains(source))
            {
                object?instance = Resources[source];
                if (targetType.IsInstanceOfType(instance))
                {
                    if (!InstanceCounts.ContainsKey(targetType))
                    {
                        InstanceCounts.Add(targetType, 1);
                    }
                    else
                    {
                        InstanceCounts[targetType]++;
                    }

                    return(instance);
                }
            }

            foreach (Type?type in FactoryFunctions.Keys)
            {
                if (type.IsAssignableFrom(targetType))
                {
#pragma warning disable CS8604 // Possible null reference argument.
                    object?instance = FactoryFunctions[type](targetType, source);
#pragma warning restore CS8604 // Possible null reference argument.
                    if (instance != null)
                    {
                        if (!InstanceCounts.ContainsKey(targetType))
                        {
                            InstanceCounts.Add(targetType, 1);
                        }
                        else
                        {
                            InstanceCounts[targetType]++;
                        }

                        if (source != null && (source is string) && IsResourceType(targetType) && !Resources.Contains(source.ToString()))
                        {
                            Resources.Add(source.ToString(), instance);
                        }
                        return(instance);
                    }
                }
            }
#pragma warning disable CS8603 // Possible null reference return.
            return(null);

#pragma warning restore CS8603 // Possible null reference return.
        }
コード例 #2
0
        public object Create(Type targetType, object source)
        {
            if (source != null && source is string)
            {
#pragma warning disable CS8604 // Possible null reference argument.
                Stream?stream = StreamFactory.Create(source.ToString());
#pragma warning restore CS8604 // Possible null reference argument.
                if (stream != null)
                {
                    object?instance = Create(targetType, stream);
                    if (instance is IDictionary idictionary)
                    {
#pragma warning disable CS8604 // Possible null reference argument.
                        idictionary.SetFileName(source.ToString());
#pragma warning restore CS8604 // Possible null reference argument.
                    }

                    return(instance);
                }
            }
            if (targetType == typeof(IDictionary) && source is Stream s)
            {
#pragma warning disable CS8603 // Possible null reference return.
                return(JSONReader.Read(s) as IDictionary);

#pragma warning restore CS8603 // Possible null reference return.
            }
            if (targetType == typeof(IList) && source is Stream s2)
            {
#pragma warning disable CS8603 // Possible null reference return.
                return(JSONReader.Read(s2) as IList);

#pragma warning restore CS8603 // Possible null reference return.
            }
#pragma warning disable CS8603 // Possible null reference return.
            return(null);

#pragma warning restore CS8603 // Possible null reference return.
        }