思路是这样
一般用反射是可以获取到对应的类型 : 既是 Form frm=new Form(); var dataType=typeof(Form);
dataType 的值就是Form
于是可以获取的对应的类型,那么怎么给这个类型去创建实例,并赋值呢?
如有一个类
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime CreateTime { get; set; }
public int CreatorId { get; set; }
public int? LastModifierId { get; set; }
public DateTime? LastModifyTime { get; set; }
}
那我现在需要通过反射的方式去创建
//获取传入的类型
Type dataType = typeof(Company);
var oResult = Activator.CreateInstance(dataType) ;
现在有了实例,需要给当前的实例赋值
//遍历属性
foreach (var propertyInfo in dataType.GetProperties())
{
//给属性赋值
propertyInfo.SetValue(oResult,value);
}
这有一个demo 一个asp.net core 3.1 控制台
c# 通过反射创建实例,并给实例赋值
这是对应的数据库脚本
CREATE TABLE [dbo].[Company](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](500) NULL,
[CreateTime] [datetime] NOT NULL,
[CreatorId] [int] NOT NULL,
[LastModifierId] [int] NULL,
[LastModifyTime] [datetime] NULL,
CONSTRAINT [PK_Company] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
INSERT INTO [dbo].[Company]
([Name]
,[CreateTime]
,[CreatorId]
,[LastModifierId]
,[LastModifyTime])
VALUES
('百捷'
,'2015-12-10'
,1
,1
,'2015-12-10')
INSERT INTO [dbo].[Company]
([Name]
,[CreateTime]
,[CreatorId]
,[LastModifierId]
,[LastModifyTime])
VALUES
('东莞'
,'2015-12-10'
,1
,1
,'2015-12-10')
INSERT INTO [dbo].[Company]
([Name]
,[CreateTime]
,[CreatorId]
,[LastModifierId]
,[LastModifyTime])
VALUES
('东风日产'
,'2018-12-10'
,1
,1
,'2018-12-10')
CREATE TABLE [dbo].[User](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
[Account] [varchar](100) NOT NULL,
[Password] [varchar](100) NOT NULL,
[Email] [varchar](200) NULL,
[Mobile] [varchar](30) NULL,
[CompanyId] [int] NULL,
[CompanyName] [nvarchar](500) NULL,
[State] [int] NOT NULL,
[UserType] [int] NOT NULL,
[LastLoginTime] [datetime] NULL,
[CreateTime] [datetime] NOT NULL,
[CreatorId] [int] NOT NULL,
[LastModifierId] [int] NULL,
[LastModifyTime] [datetime] NULL,
CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'用户状态 0正常 1冻结 2删除' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'User', @level2type=N'COLUMN',@level2name=N'State'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'用户类型 1 普通用户 2管理员 4超级管理员' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'User', @level2type=N'COLUMN',@level2name=N'UserType'
GO
INSERT INTO [dbo].[User]
([Name]
,[Account]
,[Password]
,[Email]
,[Mobile]
,[CompanyId]
,[CompanyName]
,[State]
,[UserType]
,[LastLoginTime]
,[CreateTime]
,[CreatorId]
,[LastModifierId]
,[LastModifyTime])
VALUES
('小新'
,'admin'
,'e10adc3949ba59abbe56e057f20f883e'
,'12'
,'133'
,'1'
,'百捷'
,0
,2
,'2018-12-12'
,'2018-12-12'
,1
,1
,'2018-12-12')
GO