本文整理汇总了C#中Class.getDeclaredConstructor方法的典型用法代码示例。如果您正苦于以下问题:C# Class.getDeclaredConstructor方法的具体用法?C# Class.getDeclaredConstructor怎么用?C# Class.getDeclaredConstructor使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Class
的用法示例。
在下文中一共展示了Class.getDeclaredConstructor方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetObject
/// <summary>
/// This method will instantiate an object of the provided type. If
/// the object or constructor does not have public access then this
/// will ensure the constructor is accessible and can be used.
/// </summary>
/// <param name="type">
/// this is used to ensure the object is accessible
/// </param>
/// <returns>
/// this returns an instance of the specific class type
/// </returns>
public Object GetObject(Class type) {
Constructor method = cache.get(type);
if(method == null) {
method = type.getDeclaredConstructor();
if(!method.isAccessible()) {
method.setAccessible(true);
}
cache.put(type, method);
}
return method.newInstance();
}
示例2: GetConstructor
/// <summary>
/// This is used to acquire the default no argument constructor
/// for the the provided type. If the constructor is not accessible
/// then it will be made accessible so that it can be instantiated.
/// </summary>
/// <param name="type">
/// this is the type to acquire the constructor for
/// </param>
/// <returns>
/// this returns the constructor for the type provided
/// </returns>
public Constructor GetConstructor(Class type) {
Constructor factory = type.getDeclaredConstructor();
if(!factory.isAccessible()) {
factory.setAccessible(true);
}
return factory;
}