public static Type LoadTypeInSeparateAppDomain(string typename, string assemblyname)
 {
     try
     {
         /// first try loading the type the easy way
         Type t = Type.GetType(typename, false, true);
         if (t == null)
         {
             /// it didn't load, so try loading the reference and then finding it
             /// but do it in a separate app domain so that we don't incur the wrath of the overhead monkey
             AppDomain domain = AppDomain.CreateDomain(Guid.NewGuid().ToString());
             XmlConfigurationOptionTypeUtilities loader = (XmlConfigurationOptionTypeUtilities)domain.CreateInstanceFromAndUnwrap(System.Reflection.Assembly.GetExecutingAssembly().Location, typeof(XmlConfigurationOptionTypeUtilities).FullName);
             t = loader.LoadType(typename, assemblyname);
             AppDomain.Unload(domain);
         }
         return(t);
     }
     catch (Exception ex)
     {
         Debug.WriteLine(ex);
     }
     return(null);
 }
Beispiel #2
0
        private XmlConfigurationOption ReadOption(XPathNavigator navigator, XmlConfigurationOption option)
        {
            string value = null;

            try
            {
                if (string.Compare(navigator.Name, @"Option", true) == 0)
                {
                    #region Attributes

                    XPathNavigator optionNavigator = navigator.Clone();

                    value = optionNavigator.Value;

                    if (optionNavigator.HasAttributes)
                    {
                        XPathNavigator attributesNavigator = optionNavigator.Clone();
                        if (attributesNavigator.MoveToFirstAttribute())
                        {
                            option.ElementName = attributesNavigator.Value;

                            while (attributesNavigator.MoveToNextAttribute())
                            {
                                switch (attributesNavigator.Name)
                                {
                                case @"HasChanges":
                                    option.HasChanges = XmlConvert.ToBoolean(attributesNavigator.Value);
                                    break;

                                case @"Category":
                                    option.Category = attributesNavigator.Value;
                                    break;

                                case @"Description":
                                    option.Description = attributesNavigator.Value;
                                    break;

                                case @"DisplayName":
                                    option.DisplayName = attributesNavigator.Value;
                                    break;

                                case @"Hidden":
                                    option.Hidden = XmlConvert.ToBoolean(attributesNavigator.Value);
                                    break;

                                case @"Readonly":
                                    option.Readonly = XmlConvert.ToBoolean(attributesNavigator.Value);
                                    break;

                                case @"ShouldSerializeValue":
                                    option.ShouldSerializeValue = XmlConvert.ToBoolean(attributesNavigator.Value);
                                    break;

                                case @"ValueAssemblyQualifiedName":
                                    option.ValueAssemblyQualifiedName = attributesNavigator.Value;
                                    break;

                                case @"EditorAssemblyQualifiedName":
                                    option.EditorAssemblyQualifiedName = attributesNavigator.Value;
                                    break;
                                }
                                ;
                            }
                        }
                    }

                    #endregion

                    #region Value

                    // if the option is serialized
                    if (option.ShouldSerializeValue)
                    {
                        // it should be encoded in base 64, so decode it
                        option.Value = this.GetSerializedValue(option, value);
                        return(option);
                    }

                    // otherwise figure out why type of object it is
                    Type t = XmlConfigurationOptionTypeUtilities.GetType(option);
                    if (t != null)
                    {
                        if (t.IsEnum)
                        {
                            option.Value = Enum.Parse(t, value, true);
                            return(option);
                        }

                        if (t == typeof(System.String))
                        {
                            option.Value = (string)value;
                        }
                        if (t == typeof(System.Boolean))
                        {
                            option.Value = (object)XmlConvert.ToBoolean(value);
                        }
                        if (t == typeof(System.Int32))
                        {
                            option.Value = XmlConvert.ToInt32(value);
                        }
                        if (t == typeof(System.Int64))
                        {
                            option.Value = XmlConvert.ToInt64(value);
                        }
                        if (t == typeof(System.Decimal))
                        {
                            option.Value = XmlConvert.ToDecimal(value);
                        }
                        if (t == typeof(System.Double))
                        {
                            option.Value = XmlConvert.ToDouble(value);
                        }
                        if (t == typeof(System.Byte))
                        {
                            option.Value = XmlConvert.ToByte(value);
                        }
                        if (t == typeof(System.Char))
                        {
                            option.Value = XmlConvert.ToChar(value);
                        }
                        if (t == typeof(System.DateTime))
                        {
                            option.Value = XmlConvert.ToDateTime(value, XmlDateTimeSerializationMode.Unspecified);
                        }
                        if (t == typeof(System.Guid))
                        {
                            option.Value = XmlConvert.ToGuid(value);
                        }
                        if (t == typeof(System.Int16))
                        {
                            option.Value = XmlConvert.ToInt16(value);
                        }
                        if (t == typeof(System.SByte))
                        {
                            option.Value = XmlConvert.ToSByte(value);
                        }
                        if (t == typeof(System.Single))
                        {
                            option.Value = XmlConvert.ToSingle(value);
                        }
                        if (t == typeof(System.UInt16))
                        {
                            option.Value = XmlConvert.ToUInt16(value);
                        }
                        if (t == typeof(System.UInt32))
                        {
                            option.Value = XmlConvert.ToUInt32(value);
                        }
                        if (t == typeof(System.UInt64))
                        {
                            option.Value = XmlConvert.ToUInt64(value);
                        }
                    }

                    #endregion

                    return(option);
                }
            }
            catch (Exception ex)
            {
                this.OnCannotReadValue(this, new XmlConfigurationReaderEventArgs(ex, option, value));
//				Debug.WriteLine(ex);
            }
            return(null);
        }