本文整理汇总了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);
}
示例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;
}
示例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;
}
//.........这里部分代码省略.........