本文整理汇总了C#中System.Type.GetConstructor方法的典型用法代码示例。如果您正苦于以下问题:C# Type.GetConstructor方法的具体用法?C# Type.GetConstructor怎么用?C# Type.GetConstructor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Type
的用法示例。
在下文中一共展示了Type.GetConstructor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Filter
/// <summary>
/// Создает на основе типа фильтр
/// </summary>
/// <param name="lib"></param>
/// <param name="type"></param>
public Filter(string lib, Type type)
{
libname = lib;
if (type.BaseType == typeof(AbstractFilter))
{
Exception fullex = new Exception("");
ConstructorInfo ci = type.GetConstructor(System.Type.EmptyTypes);
filter = ci.Invoke(null);
PropertyInfo everyprop;
everyprop = type.GetProperty("Name");
name = (string)everyprop.GetValue(filter, null);
everyprop = type.GetProperty("Author");
author = (string)everyprop.GetValue(filter, null);
everyprop = type.GetProperty("Ver");
version = (Version)everyprop.GetValue(filter, null);
help = type.GetMethod("Help");
MethodInfo[] methods = type.GetMethods();
filtrations = new List<Filtration>();
foreach (MethodInfo mi in methods)
if (mi.Name == "Filter")
{
try
{
filtrations.Add(new Filtration(mi));
}
catch (TypeLoadException)
{
//Не добавляем фильтрацию.
}
}
if (filtrations == null) throw new TypeIncorrectException("Класс " + name + " не содержит ни одной фильтрации");
}
else
throw new TypeLoadException("Класс " + type.Name + " не наследует AbstractFilter");
}
示例2: NullableMembers
public NullableMembers(Type elementType)
{
NullableType = NullableTypeDefinition.MakeGenericType(elementType);
Constructor = NullableType.GetConstructor(new[] { elementType });
GetHasValue = NullableType.GetProperty("HasValue").GetGetMethod();
GetValue = NullableType.GetProperty("Value").GetGetMethod();
}
示例3: ConstructOverrideInstance
private object ConstructOverrideInstance(Type overrideType, string overrideTypeName)
{
object overideStepsInstance = null;
// Invoke the override
var containerConstructor = overrideType.GetConstructor(new[] {typeof(IObjectContainer)});
if (containerConstructor == null)
{
var defaultConstructor = overrideType.GetConstructor(Type.EmptyTypes);
if (defaultConstructor == null)
{
throw new NotSupportedException(string.Format(
"No suitable constructor found on steps override type '{0}'. Type must either provide a default constructor, or a constructor that takes an IObjectContainer parameter.",
overrideTypeName));
}
// Create target type using default constructor
overideStepsInstance = defaultConstructor.Invoke(null);
}
else
{
var container = ScenarioContext.Current.GetBindingInstance(typeof(IObjectContainer)) as IObjectContainer;
overideStepsInstance = containerConstructor.Invoke(new object[] {container});
}
return overideStepsInstance;
}
示例4: Deserialize
public override object Deserialize(Stream serializationStream, Type t)
{
var arrayField = t.BaseType.GetField("_data", BindingFlags.NonPublic | BindingFlags.Instance);
var arrayType = arrayField.FieldType.GetElementType();
var sizeType = (Type) t.GetField("SizeType").GetValue(null);
var sizeFormatter = _typeFormatterFactory.GetFormatter(sizeType);
var rawSize = sizeFormatter.Deserialize(serializationStream, sizeType);
var arraySize = PrimitiveSupport.TypeToInt(rawSize);
if (arrayType == typeof (Byte))
{
var arrayData = new byte[arraySize];
serializationStream.Read(arrayData, 0, arraySize);
var ret = t.GetConstructor(new Type[] {typeof (Byte[])}).Invoke(new object[] {arrayData});
return ret;
}
else
{
var ret = t.GetConstructor(new Type[] {typeof (ulong)}).Invoke(new object[] {(ulong)arraySize});
var array = (Array)arrayField.GetValue(ret);
var subFormatter = _typeFormatterFactory.GetFormatter(arrayType);
for (var i = 0; i < arraySize; i++)
{
array.SetValue(subFormatter.Deserialize(serializationStream, arrayType), i);
}
return ret;
}
}
示例5: Create
/// <summary>创建</summary>
/// <param name="type"></param>
/// <param name="types"></param>
/// <returns></returns>
public static ConstructorInfoX Create(Type type, Type[] types)
{
ConstructorInfo constructor = type.GetConstructor(types);
if (constructor == null) constructor = type.GetConstructor(DefaultBinding, null, types, null);
if (constructor != null) return Create(constructor);
//ListX<ConstructorInfo> list = TypeX.Create(type).Constructors;
//if (list != null && list.Count > 0)
//{
// if (types == null || types.Length <= 1) return list[0];
// ListX<ConstructorInfo> list2 = new ListX<ConstructorInfo>();
// foreach (ConstructorInfo item in list)
// {
// ParameterInfo[] ps = item.GetParameters();
// if (ps == null || ps.Length < 1 || ps.Length != types.Length) continue;
// for (int i = 0; i < ps.Length; i++)
// {
// }
// }
//}
//// 基本不可能的错误,因为每个类都会有构造函数
//throw new Exception("无法找到构造函数!");
return null;
}
示例6: Show
/// <summary>
/// Shows the specified tool window by provide it's type. This function
/// has no effect if the tool window is already visible.
/// </summary>
/// <param name="window">The type of tool window to be shown.</param>
public void Show(Type window)
{
foreach (Tool d in this.m_Windows)
{
if (d.GetType() == window)
return;
}
System.Reflection.ConstructorInfo constructor = null;
Tool c = null;
// Construct the new tool window.
constructor = window.GetConstructor(new Type[] { });
if (constructor == null)
{
constructor = window.GetConstructor(new Type[] { typeof(Tools.Manager) });
c = constructor.Invoke(new object[] { this }) as Tool;
}
else
c = constructor.Invoke(null) as Tool;
// Add the solution loaded/unloaded events.
Central.Manager.SolutionLoaded += new EventHandler((sender, e) =>
{
c.OnSolutionLoaded();
});
Central.Manager.SolutionUnloaded += new EventHandler((sender, e) =>
{
c.OnSolutionUnloaded();
});
Central.Manager.IDE.ShowDock(c, c.DefaultState);
this.m_Windows.Add(c);
}
示例7: Parse
private static void Parse(Type Match, NetIncomingMessage Message)
{
if (Match.GetConstructor(new Type[0]) == null) {
throw new Exception("Message '" + Match.Name + "' Does not have an empty constructor!");
}
Message msg = (Message)Match.GetConstructor(new Type[0]).Invoke(new object[0]);
msg.Receive(Message);
}
示例8: ContentType
public ContentType(Type type)
{
Type = type.AssertNotNull();
Type.HasAttr<ContentTypeAttribute>().AssertTrue();
(Type.Attr<ContentTypeAttribute>().Token != "binary").AssertTrue();
Type.HasAttr<ContentTypeLocAttribute>().AssertTrue();
Type.GetConstructor(typeof(IBranch).MkArray());
Type.GetConstructor(typeof(IValue).MkArray());
}
示例9: ProviderInfo
public ProviderInfo( Assembly assembly, Type type )
{
this.assembly = assembly;
providerType = type;
// find normal constructor (connectionstring only)
providerConstructor = type.GetConstructor( new[] { typeof(string) } );
Check.VerifyNotNull( providerConstructor, Error.InvalidProviderLibrary, assembly.FullName );
// also try to find a constructor for when schema is specified
providerSchemaConstructor = type.GetConstructor( new[] { typeof(string), typeof(string) } );
}
示例10: ExceptionMappingAttribute
/// <summary>
/// Initializes a new instance of the <see cref="ExceptionMappingAttribute"/> class.
/// </summary>
/// <param name="exceptionType">Type of the exception.</param>
/// <param name="faultDetailType">Type of the fault detail.</param>
public ExceptionMappingAttribute(Type exceptionType,
Type faultDetailType)
{
ExceptionType = exceptionType;
FaultType = faultDetailType;
exceptionConstructor = FaultType.GetConstructor(new Type[] { exceptionType });
if (exceptionConstructor == null)
parameterlessConstructor = FaultType.GetConstructor(Type.EmptyTypes);
}
示例11: setParser
public void setParser(Type lexerclass, Type parserclass, String mainmethodname, Type tokentypesclass)
{
lexerStreamConstructorInfo = lexerclass.GetConstructor(new Type[] { typeof(Stream) });
if (lexerStreamConstructorInfo == null) throw new ArgumentException("Lexer class invalid (no constructor for Stream)");
lexerStringConstructorInfo = lexerclass.GetConstructor(new Type[] { typeof(TextReader) });
if (lexerStringConstructorInfo == null) throw new ArgumentException("Lexer class invalid (no constructor for TextReader)");
parserConstructorInfo = parserclass.GetConstructor(new Type[] { typeof(TokenStream) });
if (parserConstructorInfo == null) throw new ArgumentException("Parser class invalid (no constructor for TokenStream)");
mainParsingMethodInfo = parserclass.GetMethod(mainmethodname);
typeMap = GetTypeMap(tokentypesclass);
}
示例12: Load
public override bool Load(Queue<Token> tokens, Type tokenizerType, TemplateGroup group)
{
Token t = tokens.Dequeue();
Match m = _reg.Match(t.Content);
Tokenizer tok = (Tokenizer)tokenizerType.GetConstructor(new Type[] { typeof(string) }).Invoke(new object[] { m.Groups[2].Value });
_eval = tok.TokenizeStream(group)[0];
tok = (Tokenizer)tokenizerType.GetConstructor(new Type[] { typeof(string) }).Invoke(new object[] { m.Groups[4].Value });
_search = tok.TokenizeStream(group)[0];
tok = (Tokenizer)tokenizerType.GetConstructor(new Type[] { typeof(string) }).Invoke(new object[] { m.Groups[6].Value });
_replace = tok.TokenizeStream(group)[0];
return true;
}
示例13: IsCollection
public override bool IsCollection(Type collectionType)
{
// Implements ICollection and has a constructor that takes a single element of type ICollection
if ((typeof(ICollection).IsAssignableFrom(collectionType)
&& (collectionType.GetConstructor(new Type[] { typeof(ICollection) }) != null)))
return true;
Type ienumGeneric = collectionType.GetInterface(typeof(IEnumerable<>).Name);
if (ienumGeneric != null && collectionType.GetConstructor(new Type[] { ienumGeneric }) != null)
return true;
else
return false;
}
示例14: CreateInstanceOf
internal static object CreateInstanceOf(Type type, Random rndGen, CreatorSettings creatorSettings)
{
if (!creatorSettings.EnterRecursion())
{
return null;
}
if (rndGen.NextDouble() < creatorSettings.NullValueProbability && !type.IsValueType)
{
return null;
}
// Test convention #1: if the type has a .ctor(Random), call it and the
// type will initialize itself.
ConstructorInfo randomCtor = type.GetConstructor(new Type[] { typeof(Random) });
if (randomCtor != null)
{
return randomCtor.Invoke(new object[] { rndGen });
}
// Test convention #2: if the type has a static method CreateInstance(Random, CreatorSettings), call it and
// an new instance will be returned
var createInstanceMtd = type.GetMethod("CreateInstance", BindingFlags.Static | BindingFlags.Public);
if (createInstanceMtd != null)
{
return createInstanceMtd.Invoke(null, new object[] { rndGen, creatorSettings });
}
object result = null;
if (type.IsValueType)
{
result = Activator.CreateInstance(type);
}
else
{
ConstructorInfo defaultCtor = type.GetConstructor(Type.EmptyTypes);
if (defaultCtor == null)
{
throw new ArgumentException("Type " + type.FullName + " does not have a default constructor.");
}
result = defaultCtor.Invoke(new object[0]);
}
SetPublicProperties(type, result, rndGen, creatorSettings);
SetPublicFields(type, result, rndGen, creatorSettings);
creatorSettings.LeaveRecursion();
return result;
}
示例15: BuildEntityClasses
public void BuildEntityClasses(EntityModel model)
{
_model = model;
//Initialize static fields
if (_baseEntityClass == null) {
_baseEntityClass = typeof(EntityBase);
_baseDefaultConstructor = _baseEntityClass.GetConstructor(Type.EmptyTypes);
_baseConstructor = _baseEntityClass.GetConstructor(new Type[] { typeof(EntityRecord) });
_entityBase_Record = typeof(EntityBase).GetField("Record");
_entityRecord_GetValue = typeof(EntityRecord).GetMethods().First(m => m.Name == "GetValue" && m.GetParameters()[0].ParameterType == typeof(int));
_entityRecord_SetValue = typeof(EntityRecord).GetMethods().First(m => m.Name == "SetValue" && m.GetParameters()[0].ParameterType == typeof(int));
}
//Build classes
BuildClasses();
}