コード例 #1
0
        private static T CreateObject <T>(DomNodeType domNodeType, XmlNode annotation, string attribute)
            where T : class
        {
            string typeName    = GetAnnotation(annotation, attribute);
            string paramString = string.Empty;

            if (typeName != null)
            {
                // check for params
                int colonIndex = typeName.IndexOf(':');
                if (colonIndex >= 0)
                {
                    int paramsIndex = colonIndex + 1;
                    paramString = typeName.Substring(paramsIndex, typeName.Length - paramsIndex);
                    typeName    = typeName.Substring(0, colonIndex);
                }

                // create object from type name
                Type objectType = System.Type.GetType(typeName);
                if (objectType == null)
                {
                    throw new AnnotationException("Couldn't find type " + typeName);
                }

                // initialize with params
                object           obj          = Activator.CreateInstance(objectType);
                IAnnotatedParams annotatedObj = obj as IAnnotatedParams;
                if (annotatedObj != null)
                {
                    string[] parameters;

                    if (!string.IsNullOrEmpty(paramString))
                    {
                        parameters = paramString.Split(',');
                    }
                    else
                    {
                        parameters = TryGetEnumeration(domNodeType, annotation);
                    }
                    if (parameters != null)
                    {
                        annotatedObj.Initialize(parameters);
                    }
                }

                if (!(obj is T))
                {
                    throw new AnnotationException("Object must be " + typeof(T));
                }
                return((T)obj);
            }
            else
            {
                return(null);
            }
        }
コード例 #2
0
        // This code came from Sce.Atf.Dom.PropertyDescriptor.
        private object CreateObject(string typeName)
        {
            string paramString = string.Empty;

            if (!string.IsNullOrEmpty(typeName))
            {
                // check for params
                int colonIndex = typeName.IndexOf(':');
                if (colonIndex >= 0)
                {
                    int paramsIndex = colonIndex + 1;
                    paramString = typeName.Substring(paramsIndex, typeName.Length - paramsIndex);
                    typeName    = typeName.Substring(0, colonIndex);
                }

                // create object from type name
                Type objectType = Type.GetType(typeName);
                if (objectType == null)
                {
                    throw new InvalidOperationException("Couldn't find type " + typeName);
                }

                // initialize with params
                object           obj          = Activator.CreateInstance(objectType);
                IAnnotatedParams annotatedObj = obj as IAnnotatedParams;
                if (annotatedObj != null)
                {
                    string[] parameters = null;

                    if (!string.IsNullOrEmpty(paramString))
                    {
                        parameters = paramString.Split(',');
                    }
                    //else
                    //    parameters = TryGetEnumeration(domNodeType, annotation);
                    if (parameters != null)
                    {
                        annotatedObj.Initialize(parameters);
                    }
                }

                return(obj);
            }
            else
            {
                return(null);
            }
        }