当前位置: 首页>>代码示例>>C#>>正文


C# Type.GetConstructors方法代码示例

本文整理汇总了C#中IKVM.Reflection.Type.GetConstructors方法的典型用法代码示例。如果您正苦于以下问题:C# Type.GetConstructors方法的具体用法?C# Type.GetConstructors怎么用?C# Type.GetConstructors使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IKVM.Reflection.Type的用法示例。


在下文中一共展示了Type.GetConstructors方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetConstructors

 internal static ConstructorInfo[] GetConstructors(Type type, bool nonPublic)
 {
     return type.GetConstructors(
         nonPublic ? BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
                   : BindingFlags.Instance | BindingFlags.Public);
 }
开发者ID:2asoft,项目名称:SpaceEngineers,代码行数:6,代码来源:Helpers.cs

示例2: GetConstructors

			internal static void GetConstructors(Type type, out ConstructorInfo defCtor, out ConstructorInfo singleOneArgCtor)
			{
				defCtor = null;
				int oneArgCtorCount = 0;
				ConstructorInfo oneArgCtor = null;
				ConstructorInfo[] constructors = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
				// HACK we have a special rule to make some additional custom attributes from mscorlib usable:
				// Attributes that have two constructors, one an enum and another one taking a byte, short or int,
				// we only expose the enum constructor.
				if (constructors.Length == 2 && type.Assembly == Types.Object.Assembly)
				{
					ParameterInfo[] p0 = constructors[0].GetParameters();
					ParameterInfo[] p1 = constructors[1].GetParameters();
					if (p0.Length == 1 && p1.Length == 1)
					{
						Type t0 = p0[0].ParameterType;
						Type t1 = p1[0].ParameterType;
						bool swapped = false;
						if (t1.IsEnum)
						{
							Type tmp = t0;
							t0 = t1;
							t1 = tmp;
							swapped = true;
						}
						if (t0.IsEnum && (t1 == Types.Byte || t1 == Types.Int16 || t1 == Types.Int32))
						{
							if (swapped)
							{
								singleOneArgCtor = constructors[1];
							}
							else
							{
								singleOneArgCtor = constructors[0];
							}
							return;
						}
					}
				}
				if (type.Assembly == Types.Object.Assembly)
				{
					if (type.FullName == "System.Runtime.CompilerServices.MethodImplAttribute")
					{
						foreach (ConstructorInfo ci in constructors)
						{
							ParameterInfo[] p = ci.GetParameters();
							if (p.Length == 1 && p[0].ParameterType.IsEnum)
							{
								singleOneArgCtor = ci;
								return;
							}
						}
					}
				}
				foreach (ConstructorInfo ci in constructors)
				{
					ParameterInfo[] args = ci.GetParameters();
					if (args.Length == 0)
					{
						defCtor = ci;
					}
					else if (args.Length == 1)
					{
						if (IsSupportedType(args[0].ParameterType))
						{
							oneArgCtor = ci;
							oneArgCtorCount++;
						}
						else
						{
							// set to two to make sure we don't see the oneArgCtor as viable
							oneArgCtorCount = 2;
						}
					}
				}
				singleOneArgCtor = oneArgCtorCount == 1 ? oneArgCtor : null;
			}
开发者ID:Semogj,项目名称:ikvm-fork,代码行数:77,代码来源:DotNetTypeWrapper.cs

示例3: GenerateCode

        void GenerateCode(Type type)
        {
            if (type.IsGenericType && !type.IsGenericTypeDefinition)
                return; // generate nothing.

            var implprops = new List<PropertyInfo> ();
            var miscprops = new List<PropertyInfo> ();
            foreach (var p in type.GetProperties (BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) {
                if (targets.Contains (p.PropertyType) && !p.PropertyType.IsEnum)
                    implprops.Add (p);
                else
                    miscprops.Add (p);
            }

            output.WriteLine ("// generic ordinal type for " + type);
            string template = @"
            public {10}partial class {0} : {1} {2}
            {{
            {7} impl;

            // constructor, if not abstract.
            {9}

            // impl-to-wrapper constructor
            internal protected {8} ({7} impl) {6}
            {{
            this.impl = impl;
            {11}
            Initialize ();
            }}

            // initializer
            void Initialize ()
            {{
            // repeat for all auto (new-Android-type) properties
            {3}
            }}

            // explicit conversion operator
            public static explicit operator {7} ({0} source)
            {{
            return source.impl;
            }}

            // For a property whose type is wrapper for Android type, just make it auto property (use private set for get-only ones)
            {4}

            // Non-Android properties follow.
            {5}
            }}
            ";

            var actx = android_ass.GetType ("Android.Content.Context");
            var aaset = android_ass.GetType ("Android.Util.IAttributeSet");
            // somehow GetConstructor() returns weird results, so this workarounds that.
            string args =
                type.GetConstructors ().Any (c => c.GetParameters ().Length == 1 && c.GetParameters () [0].ParameterType.FullName == actx.FullName) ? "XamlView.CurrentContext" :
                type.GetConstructors ().Any (c => c.GetParameters ().Length == 2 && c.GetParameters () [0].ParameterType.FullName == actx.FullName && c.GetParameters () [1].ParameterType.FullName == aaset.FullName) ? "XamlView.CurrentContext, null" :
                "";
            //if (type.Name == "SlidingDrawer") foreach (var ccc in type.GetConstructors ()) Console.WriteLine ("!!!! " + ccc + " / " +  type.GetConstructor (new Type [] {actx}).GetParameters ().Length);
            string publicConstructor = type.IsAbstract ? String.Empty : String.Format (@"
            public {0} ()
            : this (new {1} ({2}))
            {{

            }}", type.NonGenericName (), type.CSFullName (), args);

            string templateInit = @"
            if (impl.{0} != null)
                {0} = Extensions.GetWrappedItem<{1}> (impl.{0});";
            var isw = new StringWriter () { NewLine = "\n" };
            foreach (var p in implprops)
                if (!p.IsAbstract ())
                    isw.WriteLine (templateInit, p.Name, p.PropertyType.CSName ());

            string templateImplProp = @"
            public {3}{0} {1} {{ get; {2}set; }}";
            var dpsw = new StringWriter () { NewLine = "\n" };
            foreach (var p in implprops)
                dpsw.WriteLine (templateImplProp, p.PropertyType.CSSwitchName (), p.Name, p.IsSetterPublic () ? null : "internal ", GetModifier (p));

            string templateOrdProp1 = @"
            public {3}{0} {1} {{
            get {{ return {4}; }}
            {2}
            }}";
            string templateOrdProp2 = @"
            public {3}{0} {1} {{ get; {5} }}";
            var nsw = new StringWriter () { NewLine = "\n" };
            foreach (var p in miscprops) {
                var setter = String.Format ("set {{ impl.{0} = value; }}", p.Name);
                nsw.WriteLine (p.IsAbstract () ? templateOrdProp2 : templateOrdProp1, p.PropertyType.CSSwitchName (), p.Name, p.IsSetterPublic () ? setter : null, GetModifier (p), GetValueExpression (p), p.IsSetterPublic () ? "set;" : null);
            }

            string gconsts = null;
            foreach (var arg in type.GetGenericArguments ()) {
                var gca = String.Join (",", (from t in arg.GetGenericParameterConstraints () select t.CSSwitchName ()).ToArray ());
                gconsts += String.IsNullOrEmpty (gca) ? null : "where " + arg.Name + " : " + gca;
            }

//.........这里部分代码省略.........
开发者ID:atsushieno,项目名称:xamldroid,代码行数:101,代码来源:xamldroid-generator.cs


注:本文中的IKVM.Reflection.Type.GetConstructors方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。