protected static object LoadInstance(XElement definition, Type expectedType, object instance)
        {
            if (definition != null)
            {
                XAttribute typeName = definition.Attribute(TypeAttributeName);
                if (typeName != null)
                {
                    // Type attribute is specified, instantiate a new object of that type
                    // If configuration instance is already created with the correct type, don't create it just load its properties
                    if (instance == null || instance.GetType() != GetType(typeName.Value))
                    {
                        // Type specified, create a new instance
                        instance = CreateInstance(expectedType, typeName.Value);
                    }
                }
                else if (!definition.Elements().Any() && !definition.Attributes().Any())
                {
                    // Type attribute is not specified and no child elements or attributes exist, so this must be a scalar value
                    LoadInstanceFromValue(definition, expectedType, ref instance);
                }
                else if (instance == null && !expectedType.IsAbstract())
                {
                    instance = Activator.CreateInstance(expectedType);
                }
                else if (instance == null)
                {
                    throw new InvalidOperationException(
                              string.Format(
                                  CultureInfo.CurrentCulture,
                                  "'{0}' element does not have a Type attribute, does not specify a value and is not a valid collection type",
                                  definition.Name.LocalName));
                }

                if (instance != null)
                {
                    LoadProperties(definition, instance);
                    Type elementType;
                    if (GetCollectionElementType(instance.GetType(), out elementType))
                    {
                        MethodInfo genericLoadInstances = LoadInstancesDefinition.MakeGenericMethod(elementType);
                        genericLoadInstances.Invoke(null, new object[] { definition, instance });
                    }
                }
            }

            return(instance);
        }
Exemplo n.º 2
0
        protected static object LoadInstance(XElement definition, Type expectedType, object instance, object[] constructorArgs, TelemetryModules modules)
        {
            if (definition != null)
            {
                XAttribute typeName = definition.Attribute(TypeAttributeName);
                if (typeName != null)
                {
                    // Type attribute is specified, instantiate a new object of that type
                    // If configuration instance is already created with the correct type, don't create it just load its properties
                    if (instance == null || instance.GetType() != GetType(typeName.Value))
                    {
                        // Type specified, create a new instance
                        instance = CreateInstance(expectedType, typeName.Value, constructorArgs);
                    }
                }
                else if (!definition.Elements().Any() && !definition.Attributes().Any() && constructorArgs == null)
                {
                    // Neither type attribute nor constructor args are specified, and no child elements or attributes exist, so this must be a scalar value.
                    LoadInstanceFromValue(definition, expectedType, ref instance);
                }
                else if (instance == null && !expectedType.IsAbstract())
                {
                    instance = constructorArgs != null?Activator.CreateInstance(expectedType, constructorArgs) : Activator.CreateInstance(expectedType);
                }
                else if (instance == null)
                {
                    CoreEventSource.Log.IncorrectInstanceAtributesConfigurationError(definition.Name.LocalName);
                }

                if (instance != null)
                {
                    LoadProperties(definition, instance, modules);
                    Type elementType;
                    if (GetCollectionElementType(instance.GetType(), out elementType))
                    {
                        MethodInfo genericLoadInstances = LoadInstancesDefinition.MakeGenericMethod(elementType);
                        genericLoadInstances.Invoke(null, new[] { definition, instance, modules });
                    }
                }
            }

            return(instance);
        }