反射,给实体类添加字段

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)

如果可以的话,请提供代码,万分感谢。
问题解决,马上给分。
...全文
1919 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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
{
//加两个属性
}

111,094

社区成员

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

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

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