反射,给实体类添加字段

linjun186349 2006-04-25 03:00:16
是否可以利用给实体类,添加字段。
Public Class Form1

Private mUser As String

Public Property User() As String
Get
Return mUser
End Get
Set(ByVal value As String)
mUser = value
End Set
End Property
End Class

此类已经有了字段 user ,
是否可以利用反射,动态将form1 类添加另外两个字段dpt_id (string), position_id(string)

如果可以的话,请提供代码,万分感谢。
问题解决,马上给分。
...全文
1862 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
lookatliu 2006-08-15
  • 打赏
  • 举报
回复
动态编译可以满足你的要求
linjun186349 2006-04-25
  • 打赏
  • 举报
回复
决定由此建立一个完整的新的实体类

linjun186349 2006-04-25
  • 打赏
  • 举报
回复
晕,没有高手在么??

在MSDN找到一下代码,用来动态添加属性, 它新建了一个类,但是没有找到
在现有的类中添加属性的资料。。


Imports System
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit

Class PropertyBuilderDemo

Public Shared Function BuildDynamicTypeWithProperties() As Type
Dim myDomain As AppDomain = Thread.GetDomain()
Dim myAsmName As New AssemblyName()
myAsmName.Name = "MyDynamicAssembly.dll"

' To generate a persistable assembly, specify AssemblyBuilderAccess.RunAndSave.
Dim myAsmBuilder As AssemblyBuilder = myDomain.DefineDynamicAssembly(myAsmName, _
AssemblyBuilderAccess.Run)

' To generate a persistable module, use DefineDynamicModule(myAsmName.Name, myAsmName.Name).
Dim myModBuilder As ModuleBuilder = myAsmBuilder.DefineDynamicModule("MyModule")

Dim myTypeBuilder As TypeBuilder = myModBuilder.DefineType("CustomerData", TypeAttributes.Public)

Dim customerNameBldr As FieldBuilder = myTypeBuilder.DefineField("customerName", _
GetType(String), FieldAttributes.Private)

' The last argument of DefineProperty is Nothing, because the
' property has no parameters. (If you don't specify Nothing, you must
' specify an array of Type objects. For a parameterless property,
' use an array with no elements: New Type() {})
Dim custNamePropBldr As PropertyBuilder = myTypeBuilder.DefineProperty("CustomerName", _
PropertyAttributes.HasDefault, _
GetType(String), _
Nothing)

' First, we'll define the behavior of the "get" property for CustomerName as a method.
Dim custNameGetPropMthdBldr As MethodBuilder = myTypeBuilder.DefineMethod("GetCustomerName", _
MethodAttributes.Public, GetType(String), _
New Type() {})

Dim custNameGetIL As ILGenerator = custNameGetPropMthdBldr.GetILGenerator()

custNameGetIL.Emit(OpCodes.Ldarg_0)
custNameGetIL.Emit(OpCodes.Ldfld, customerNameBldr)
custNameGetIL.Emit(OpCodes.Ret)

' Now, we'll define the behavior of the "set" property for CustomerName.
Dim custNameSetPropMthdBldr As MethodBuilder = myTypeBuilder.DefineMethod("SetCustomerName", _
MethodAttributes.Public, Nothing, _
New Type() {GetType(String)})

Dim custNameSetIL As ILGenerator = custNameSetPropMthdBldr.GetILGenerator()

custNameSetIL.Emit(OpCodes.Ldarg_0)
custNameSetIL.Emit(OpCodes.Ldarg_1)
custNameSetIL.Emit(OpCodes.Stfld, customerNameBldr)
custNameSetIL.Emit(OpCodes.Ret)

' Last, we must map the two methods created above to our PropertyBuilder to
' their corresponding behaviors, "get" and "set" respectively.
custNamePropBldr.SetGetMethod(custNameGetPropMthdBldr)
custNamePropBldr.SetSetMethod(custNameSetPropMthdBldr)

Return myTypeBuilder.CreateType()
End Function 'BuildDynamicTypeWithProperties


Public Shared Sub Main()
Dim custDataType As Type = BuildDynamicTypeWithProperties()

Dim custDataPropInfo As PropertyInfo() = custDataType.GetProperties()
Dim pInfo As PropertyInfo
For Each pInfo In custDataPropInfo
Console.WriteLine("Property '{0}' created!", pInfo.ToString())
Next pInfo

Console.WriteLine("---")
' Note that when invoking a property, you need to use the proper BindingFlags -
' BindingFlags.SetProperty when you invoke the "set" behavior, and
' BindingFlags.GetProperty when you invoke the "get" behavior. Also note that
' we invoke them based on the name we gave the property, as expected, and not
' the name of the methods we bound to the specific property behaviors.
Dim custData As Object = Activator.CreateInstance(custDataType)
custDataType.InvokeMember("CustomerName", BindingFlags.SetProperty, Nothing, _
custData, New Object() {"Joe User"})

Console.WriteLine("The customerName field of instance custData has been set to '{0}'.", _
custDataType.InvokeMember("CustomerName", BindingFlags.GetProperty, _
Nothing, custData, New Object() {}))
End Sub 'Main
End Class 'PropertyBuilderDemo


' --- O U T P U T ---
' The output should be as follows:
' -------------------
' Property 'System.String CustomerName [System.String]' created!
' ---
' The customerName field of instance custData has been set to 'Joe User'.
' -------------------

mapserver 2006-04-25
  • 打赏
  • 举报
回复
做不到
反射的原理是去“读”自描述的源数据,而不能去“更改”。
wshcdr 2006-04-25
  • 打赏
  • 举报
回复
反射的功能与楼主的需求没有逻辑上的联系
linjun186349 2006-04-25
  • 打赏
  • 举报
回复
因为我的数据是从数据库读取出来的,字段的数量是动态的,
我需要把结果集转换为list<Of T>
Knight94 2006-04-25
  • 打赏
  • 举报
回复
动态基本上是不可能的,除非你的form1提供hashtable的话,还可以模拟动态加载字段操作。
zahuifan 2006-04-25
  • 打赏
  • 举报
回复
用反射恐怕无法动态添加字段。我理解搂住的需要,但是确实很难做到。
real3000 2006-04-25
  • 打赏
  • 举报
回复
关注
webwait 2006-04-25
  • 打赏
  • 举报
回复
mark
WTaoboy 2006-04-25
  • 打赏
  • 举报
回复
up
linjun186349 2006-04-25
  • 打赏
  • 举报
回复
晕,看来高手是不会来这里的。。
ChrisAK 2006-04-25
  • 打赏
  • 举报
回复
继承
aSalt 2006-04-25
  • 打赏
  • 举报
回复
必须用反射吗?
不用反射,继承是不是更好点

//不同程序集要引用
public class example : Form1
{
//加两个属性
}
Elasticsearch 简介 ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。elasticSearch 的使用场景 1、在海量数据前提下,对数据进行检索。比如:京东,淘宝等电商项目课程目标: 1. 了解企业级搜索引擎2. 安装elasticsearch 课程目录: 01 课程介绍02 elasticsearch 简介03 elasticsearch 使用场景04 安装elasticsearch 之前先安装jdk05 安装elasticsearch06 测试elasticsearch是否安装成功 07 安装kibana08 elasticsearch 基本认识 以及添加索引和删除索引09 elasticsearch 添加查询数据10 elasticsearch 修改删除数据11 elasticsearch 有条件的查询12 分词子属性fuzzy查询13 elasticsearch 过滤使用14 elasticsearch 排序与分页15 elasticsearch 如何查询指定的字段16 elasticsearch 高亮显示17 elasticsearch 聚合18 elasticsearch mapping 概念19 elasticsearch 的中文词库20 elasticsearch 中文词库安装测试21 elasticsearch 中文词库的使用案例22 elasticsearch 自定义词库配置23 安装nginx 配置中文词库24 测试elasticsearch 自定义中文词库25 搭建项目父工程26 搭建项目bean-interface-common27 搭建search 的service web 项目28 测试项目是否能与elasticsearch联通29 创建数据库并搭建首页30 数据上传功能的实现类完成31 数据上传控制器完成32 dubbo 介绍以及安装zookeeper33 将数据从mysql 上传到elasticsearch 中34 elasticsearch查询功能分析35 编写业务需求的dsl 语句36 编写输入参数返回结果集的实体类37 实现类编写38 编写实现类中dsl 语句39 返回集结果转换40 结果测试41 测试通过输入查询条件并将数据显示到页面

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧