zoukankan      html  css  js  c++  java
  • 反射学习

    Civil 3D样例文件中关于样式比较的例子,

    里面采用了反射,

    快速的访问数据库中的每一种样式,

    是一个很好的例子。

       

    昨天自己折腾了快一天,

    才完成的代码,

    没想到在这个样例里居然都可以直接找到。

    看来有时间应该把每一个样例都阅读一遍。

       

    using System;
    

    using System.Collections.Generic;
    

    using System.Linq;
    

    using System.Text;
    

    using Autodesk.AutoCAD.Runtime;
    

    using Autodesk.Civil.ApplicationServices;
    

    using Autodesk.AutoCAD.EditorInput;
    

    using Autodesk.AutoCAD.ApplicationServices;
    

    using Autodesk.AutoCAD.Windows;
    

    using Autodesk.Civil.DatabaseServices.Styles;
    

    using System.Reflection;
    

    using Autodesk.AutoCAD.DatabaseServices;
    

    using System.IO;
    

    using System.Collections;
    

    using System.Collections.Specialized;
    

    using System.Diagnostics;
    

     

    namespace CompareStyles {
    

    
    					///
    					<summary>
    				

    
    				/// This example illustrates the Civil 3D styles API.
    

    
    					/// Styles are organized in a way that makes it easy to examine them
    

    
    					/// using .NET reflection.  A Civil document contains all styles in a StylesRoot
    

    
    					/// object.  This object contains collections of styles, and other
    

    
    					/// "root" objects for groups of related styles (such as TableStyles).
    

    
    					///
    				

    
    				/// All collections of styles derive from StyleCollectionBase, and
    

    
    					/// all styles derive from StyleBase.
    

    
    					///
    					</summary>
    				

    
    				public
    				class CompareStyles : IExtensionApplication {
    

     

    
    					private
    					static Transaction ts;
    

     

    
    					///
    					<summary>
    				

    
    				/// Command to show our dialog for comparing the styles.
    

    
    					///
    					</summary>
    				

            [CommandMethod("CompareStyles")]
    

    
    					public
    					static
    					void ShowDlg () {
    

                StyleFrm sf = new StyleFrm();
    

                sf.Show();
    

     

            }
    

     

    
    					///
    					<summary>
    				

    
    				/// Gets the styles for the currently open document, puts the information
    

    
    					/// for each style into an StyleInfo object, and returns an ArrayList of
    

    
    					/// the StyleInfo objects.
    

    
    					///
    					</summary>
    				

    
    				///
    				<returns></returns>
    			

    
    				public
    				static ArrayList GetStyles () {
    

     

                ArrayList styleList = new ArrayList();
    

     

    
    					using ( ts = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.
    

                  Database.TransactionManager.StartTransaction() ) {
    

                    CivilDocument CivilDoc = Autodesk.Civil.ApplicationServices.CivilApplication.ActiveDocument;
    

                    ListRoot(CivilDoc.Styles, styleList);
    

                }
    

     

                ts.Dispose();
    

     

    
    					return styleList;
    

            }
    

     

     

     

    
    					///
    					<summary>
    				

    
    				///  Looks at a "root" object for styles.  Each root class contains a group of
    

    
    					///  collections (derived from StyleBaseCollection), or other style root objects.
    

    
    					///
    					</summary>
    				

    
    				///
    				<param name="root">The style root object to look at.</param>
    					

    
    				private
    				static
    				void ListRoot ( object root, ArrayList styleList ) {
    

    
    					// Get all the properties
    				

                Type objectType = root.GetType();
    

                PropertyInfo[] properties = objectType.GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance);
    

     

    
    					foreach ( PropertyInfo pf in properties ) {
    

     

    
    					// If it's a collection, let's iterate through it
    				

    
    				if ( pf.PropertyType.ToString().Contains("Collection") ) {
    

    
    					// Debug.WriteLine(String.Format("Processing collection: {0}", pf.Name));
    				

                        ListCollection(objectType, pf, root, styleList);
    

                    } else
    					if ( pf.PropertyType.ToString().Contains("Root") ) {
    

    
    					// Call ourselves recursively on this style root object                    
    				

    
    				object root2 = objectType.InvokeMember(pf.Name,
    

                                BindingFlags.GetProperty, null, root, new
    					object[0]);
    

    
    					if ( root2.Equals(null) )
    

    
    					return;
    

                        ListRoot(root2, styleList);
    

                    } else
    					if ( pf.PropertyType.ToString().Contains("Default") ) {
    

    
    					// A default type, just use the name
    				

                    } else {
    

    
    					// We're not sure what this is
    				

     

                    }
    

                }
    

            }
    

     

    
    					///
    					<summary>
    				

    
    				/// Iterates through a style collection (derived from StyleCollectionBase), and
    

    
    					/// prints out the name of each style it contains.
    

    
    					///
    					</summary>
    				

    
    				///
    				<param name="objectType"></param>
    			

    
    				///
    				<param name="pf"></param>
    			

    
    				///
    				<param name="myStylesRoot"></param>
    			

    
    				private
    				static
    				void ListCollection ( Type objectType, PropertyInfo pf, object myStylesRoot, ArrayList styleList ) {
    

     

    
    					object res = objectType.InvokeMember(pf.Name,
    

                                BindingFlags.GetProperty, null, myStylesRoot, new
    					object[0]);
    

    
    					if ( res.Equals(null) )
    

    
    					return;
    

     

                StyleCollectionBase scBase = ( StyleCollectionBase )res;
    

     

     

    
    					foreach ( ObjectId sbid in scBase ) {
    

                    StyleBase stylebase = ts.GetObject(sbid, OpenMode.ForRead) as StyleBase;
    

     

    
    					// Add the style name and parameters to the list of all styles
    				

                    StyleInfo styleinfo = new StyleInfo();
    

                    styleinfo.name = stylebase.Name;
    

                    styleinfo.type = stylebase.GetType().ToString();
    

    
    					// Get all the properties
    				

                    Type styleType = stylebase.GetType();
    

                    PropertyInfo[] properties = styleType.GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance);
    

     

    
    					foreach ( PropertyInfo stylePropInfo in properties ) {
    

    
    					object styleprop = null;
    

     

    
    					if ( stylePropInfo.Name.Contains("Application") || stylePropInfo.Name.Contains("Document") ) {
    

                            styleinfo.paramValues.Add(stylePropInfo.Name, "Not implemented");
    

    
    					return;
    

                        }
    

     

    
    					try {
    

                            styleprop = styleType.InvokeMember(stylePropInfo.Name,
    

                                    BindingFlags.GetProperty, null, stylebase, new
    					object[0]);
    

     

    
    					// For object ID values, we need to get the target style base
    

                            // and record the name.
    			

    
    				if ( styleprop.GetType().Name == "ObjectId" ) {
    

                                ObjectId objid = ( ObjectId )styleprop;
    

                                StyleBase proptarget = ts.GetObject(objid, OpenMode.ForRead) as StyleBase;
    

                                styleprop = proptarget.Name;
    

                            }
    

                        } catch ( System.Reflection.TargetInvocationException) {
    

    
    					// Should do something
    				

                            styleprop = "NULL";
    

                        } catch ( System.Exception) {
    

                            styleprop = "NULL";
    

                        }
    

     

    
    					if ( !styleinfo.paramValues.ContainsKey(stylePropInfo.Name) ) {
    

                            styleinfo.paramValues.Add(stylePropInfo.Name, styleprop.ToString());
    

                        }
    

     

                    }
    

     

                    styleinfo.parent = pf.Name;
    

     

                    styleList.Add(styleinfo);
    

                }
    

     

     

            }
    

     

     

    
    					#region IExtensionApplication Members
    

    
    				public
    				void Initialize () { ShowDlg(); }
    

    
    					public
    					void Terminate () { }
    

    
    					#endregion
    						

        }
    

     

    
    					///
    					<summary>
    				

    
    				/// A class to store style information
    

    
    					///
    					</summary>
    				

    
    				public
    				class StyleInfo {
    

    
    					public String parent; // Name of the collection holding the style
    				

    
    				public String name; // Name of the style
    			

    
    				public String type; // Type of the style, not currently used
    			

    
    				public Dictionary<String, String> paramValues; // dictionary of style parameters, name/value pairs
    			

     

    
    				public StyleInfo () {
    

                paramValues = new Dictionary<string, string>();
    

            }
    

     

    
    					public String toString () {
    

                String val = String.Format("
    
    *Parent: {0} Style: {1} (type {2})
    ", parent, name, type);
    

    
    					foreach ( KeyValuePair<string, string> kp in paramValues ) {
    

                    val += String.Format("   Param: {0}: {1}
    ", kp.Key, kp.Value);
    

                }
    

    
    					return val;
    

            }
    

        }
    

    }
    			

      

  • 相关阅读:
    [转载]ASP.NET中IsPostBack详解
    [转载]论asp.net out、ref、return
    用CSS让字体在一行内显示不换行
    改变时间格式的方法
    JS方法的使用
    [转载] iframe嵌入网页的用法
    iphone6 inline-flex兼容问题
    ActionResult的其它返回值
    为什么java中只允许继承一个类?
    关于asp.net MVC3 ----@Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction
  • 原文地址:https://www.cnblogs.com/myzw/p/10002217.html
Copyright © 2011-2022 走看看