-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathActivatorExtesnions.cs
More file actions
42 lines (38 loc) · 1.31 KB
/
ActivatorExtesnions.cs
File metadata and controls
42 lines (38 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Copyright (c) BootstrapBlazor & Argo Zhang (argo@live.ca). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/
using System.Reflection;
namespace System;
/// <summary>
/// Activator 扩展方法
/// </summary>
public static class ActivatorExtesnions
{
/// <summary>
/// 通过指定类型与参数创建实例方法
/// </summary>
/// <param name="type"></param>
/// <param name="args"></param>
/// <returns></returns>
public static object? CreateInstance(this Type type, object?[]? args = null)
{
var bindings = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Default;
return Activator.CreateInstance(type, bindings, null, args, null);
}
/// <summary>
/// 通过指定类型与参数创建实例方法
/// </summary>
/// <param name="type"></param>
/// <param name="args"></param>
/// <returns></returns>
public static TType? CreateInstance<TType>(this Type type, object?[]? args = null)
{
TType? ret = default;
var value = type.CreateInstance(args);
if (value is TType v)
{
ret = v;
}
return ret;
}
}