按名称设置属性 [英] Setting properties by name

查看:63
本文介绍了按名称设置属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好


我想写一个方法,允许我传递一个实例的引用

一个类,一个属性的名称该类和一个值设置

属性,该方法然后将实例的属性设置为值


这里是一个示例该方法可能是什么样的


public void SetProperty(对象实例,字符串属性,对象值)

{

// TODO:设置实例的命名属性值为

}


方法可以像这样调用


SetProperty(myClassInstance," MyProperty"," My Value");


如何编写这样的方法?如何以后期方式访问实例的成员

,或者甚至可以将引用传递给实际的

属性,如此


SetProperty(myClassInstance,myClassInstance.MyProperty," The Value");


其中方法没有取myClassInstance.MyProperty的值而是

对实际属性的引用,以便它可以设置它的值,这可能吗?


谢谢


Alex

解决方案

反思:


instance.GetType()。GetProperty(property).SetValue(instance, value,null);


(假设是公共的,可写的,实例属性)


Marc

Alexander,


查看.net反射。


设置属性的代码如下所示:


类型t = myClassInstance.GetType();

正确tyInfo pi = t.GetProperty(MyProperty);

pi.SetValue(myClassInstance,myValue,null);


Type.GetProperty方法有多个重载。根据房产的可见度,您可能需要使用其他的一些




如果您想要传递房产的实例。你可以使用PropertyInfo

类。


-

HTH

Stoitcho Goutsev(100 )


" Alexander Walker" <人** @ noemail.noemail>在消息中写道

新闻:%2 ****************** @ TK2MSFTNGP09.phx.gbl ...

您好

我想编写一个方法,允许我传递对类的
实例的引用,该类的属性名称和值设置
属性,然后该方法将实例的属性设置为



这是一个方法可能是什么样子的例子

public void SetProperty(对象实例,字符串属性,对象值)
// TODO:设置实例的命名属性值为
}

SetProperty(myClassInstance,MyProperty,我的价值);

如何编写这样的方法?你如何以后期的方式访问
实例的成员,或者甚至可以传递对
实际
属性的引用,如此

SetProperty(myClassInstance,myClassInstance.MyProperty," The Value");

其中方法没有取myClassInstance.MyProperty的值但

参考对于实际的属性,以便它可以设置它的价值,这是否可能?

谢谢

Alex



以下是我用来完成的一些方法(包括Set和Get)。

他们在VB.NET中,所以你我必须翻译它们:


''描述:

''使用反射为对象上的属性调用get方法。

GetProperty

''搜索继承链,直到找到匹配为止。所有访问

级别

''(私人,公共等)支持

''参数:

' 'name - 要获得的财产的名称

''对象 - 获取财产价值的对象

''params - 财产的可选参数

''返回值:

''物业的获取方法的返回值

''例外:

' 'MissingMemberException - 没有找到匹配

指定名称或参数的属性或get方法

''MissingMethodException - 找不到get方法(属性是

只写)

公共共享函数GetProperty(ByVal name As String,ByVal obj As

Object,ByVal ParamArray params()As Object)As Object

Dim objectType As Type = obj.GetType

Dim flags As BindingFlags = BindingFlags.Instance或_

BindingFlags.Static或_

毕ndingFlags.NonPublic或_

BindingFlags.Public


Dim propertyInfo As PropertyInfo

Dim types(params.Length - 1)As类型

Dim index As Integer

索引= 0 to params.Length - 1

types(index)= params(index).GetType

下一页


虽然不是objectType什么都没有

propertyInfo = objectType.GetProperty(name,flags,Nothing,Nothing,

类型,没什么)

如果propertyInfo是Nothing那么

objectType = objectType.BaseType

否则

退出时

结束如果

结束时


如果propertyInfo什么都没有那么

抛出新的MissingMemberException(obj.GetType.FullName,"")

ElseIf Not propertyInfo.CanRead然后

抛出新的MissingMethodException(obj.GetType.FullName,"" ;)

Else

返回propertyInfo.GetValue(obj,params)

结束如果

结束函数


''描述:

''使用反射为对象上的属性调用set方法。

SetProperty

''搜索继承链,直到找到匹配项。所有访问

级别

''(私人,公共等)支持

''参数:

' 'name - 要设置的属性的名称

''object - 要设置属性值的对象

''value - 要分配给属性的值

''params - 物业的可选参数

''返回值:

''没有

''例外:

''MissingMemberException - 找不到与指定名称相匹配的属性或

参数

''MissingMethodException - 未找到set方法(属性为

只读)

Public Shared Sub SetProperty(ByVal name As String,ByVal obj As Object,

ByVal value As Object,ByVal ParamArray params ()As Object)

Dim objectType As Type = obj.GetType

Dim flags As BindingFlags = BindingFlags.Instance或_

BindingFlags.Static或者_

BindingFlags.NonPublic或_

BindingFlags.Public


Dim propertyInfo As PropertyInfo

Dim types(params.Length - 1)类型

Dim index As Integer

索引= 0 to params.Length - 1

types(index)= params( ().GetType

下一页


虽然不是objectType什么都没有

propertyInfo = objectType.GetProperty(name,flags,Nothing,没什么,

类型,没什么)

如果propertyInfo什么都没有那么

objectType = objectType.BaseType

Else

退出时

结束如果

结束时


如果propertyInfo什么都没有那么

抛出新的MissingMemberException(obj.GetType.FullName,"")

ElseIf Not propertyInfo.CanWrite Then

抛出新的MissingMethodException(obj.GetType.FullName ,")

Else

propertyInfo.SetValue(obj,value,params)

结束如果

结束子

/ claes


" Alexander Walker" <人** @ noemail.noemail>在消息中写道

新闻:%2 ****************** @ TK2MSFTNGP09.phx.gbl ...

您好

我想编写一个方法,允许我传递对类的
实例的引用,该类的属性名称和值设置
属性,然后该方法将实例的属性设置为



这是一个方法可能是什么样子的例子

public void SetProperty(对象实例,字符串属性,对象值)
// TODO:设置实例的命名属性值为
}

SetProperty(myClassInstance,MyProperty,我的价值);

如何编写这样的方法?你如何以后期的方式访问
实例的成员,或者甚至可以传递对
实际
属性的引用,如此

SetProperty(myClassInstance,myClassInstance.MyProperty," The Value");

其中方法没有取myClassInstance.MyProperty的值但

参考对于实际的属性,以便它可以设置它的价值,这是否可能?

谢谢

Alex



Hello

I would like to write a method that allows me to pass a reference to an instance
of a class, the name of a property of that class and a value to set that
property to, the method would then set the property of the instance to the value

here is an example of what the method might look like

public void SetProperty(object instance, string property, object value)
{
//TODO: set the named property of the instance with the value
}

The method could be called like this

SetProperty(myClassInstance, "MyProperty", "My Value");

How would such a method be written? how do you access the members of an instance
in a late bound manner, or is it even possible to pass a reference to the actual
property like so

SetProperty(myClassInstance, myClassInstance.MyProperty, "The Value");

Where the method is not taking the value of myClassInstance.MyProperty but the
reference to the actual property so that it can set its value, is this possible?

Thanks

Alex

解决方案

Reflection:

instance.GetType().GetProperty(property).SetValue( instance, value, null);

(assumes public, writeable, instance property)

Marc


Alexander,

Look at the .net reflection.

The code for setting the property would look something like this:

Type t = myClassInstance.GetType();
PropertyInfo pi = t.GetProperty(MyProperty);
pi.SetValue(myClassInstance, myValue, null);

Type.GetProperty method has more than one overload. You may need to use some
of the others depending on the visibility of the property.

If you want to pass "instance of a property" you can use the PropertyInfo
class.

--
HTH
Stoitcho Goutsev (100)

"Alexander Walker" <al**@noemail.noemail> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...

Hello

I would like to write a method that allows me to pass a reference to an
instance
of a class, the name of a property of that class and a value to set that
property to, the method would then set the property of the instance to the
value

here is an example of what the method might look like

public void SetProperty(object instance, string property, object value)
{
//TODO: set the named property of the instance with the value
}

The method could be called like this

SetProperty(myClassInstance, "MyProperty", "My Value");

How would such a method be written? how do you access the members of an
instance
in a late bound manner, or is it even possible to pass a reference to the
actual
property like so

SetProperty(myClassInstance, myClassInstance.MyProperty, "The Value");

Where the method is not taking the value of myClassInstance.MyProperty but
the
reference to the actual property so that it can set its value, is this
possible?

Thanks

Alex



Here are some methods I''ve been using to accomplish that (both Set and Get).
They''re in VB.NET though so you''ll have to translate them:

'' Description:
'' Calls the get method for a property on an object using reflection.
GetProperty
'' searches the inheritance chain until it finds a match. All access
levels
'' (private, public etc) are supported
'' Parameters:
'' name - Name of property to get
'' object - Object on which to get the property value
'' params - Optional parameters to the property
'' Return value:
'' The returned value of the property''s get method
'' Exceptions:
'' MissingMemberException - No property or get method matching the
specified name or parameters was found
'' MissingMethodException - No get method was found (property is
write-only)
Public Shared Function GetProperty(ByVal name As String, ByVal obj As
Object, ByVal ParamArray params() As Object) As Object
Dim objectType As Type = obj.GetType
Dim flags As BindingFlags = BindingFlags.Instance Or _
BindingFlags.Static Or _
BindingFlags.NonPublic Or _
BindingFlags.Public

Dim propertyInfo As PropertyInfo
Dim types(params.Length - 1) As Type
Dim index As Integer
For index = 0 To params.Length - 1
types(index) = params(index).GetType
Next

While Not objectType Is Nothing
propertyInfo = objectType.GetProperty(name, flags, Nothing, Nothing,
types, Nothing)
If propertyInfo Is Nothing Then
objectType = objectType.BaseType
Else
Exit While
End If
End While

If propertyInfo Is Nothing Then
Throw New MissingMemberException(obj.GetType.FullName, "")
ElseIf Not propertyInfo.CanRead Then
Throw New MissingMethodException(obj.GetType.FullName, "")
Else
Return propertyInfo.GetValue(obj, params)
End If
End Function

'' Description:
'' Calls the set method for a property on an object using reflection.
SetProperty
'' searches the inheritance chain until it finds a match. All access
levels
'' (private, public etc) are supported
'' Parameters:
'' name - Name of property to set
'' object - Object on which to set the property value
'' value - Value to assign to the property
'' params - Optional parameters to the property
'' Return value:
'' None
'' Exceptions:
'' MissingMemberException - No property matching the specified name or
parameters was found
'' MissingMethodException - No set method was found (property is
read-only)
Public Shared Sub SetProperty(ByVal name As String, ByVal obj As Object,
ByVal value As Object, ByVal ParamArray params() As Object)
Dim objectType As Type = obj.GetType
Dim flags As BindingFlags = BindingFlags.Instance Or _
BindingFlags.Static Or _
BindingFlags.NonPublic Or _
BindingFlags.Public

Dim propertyInfo As PropertyInfo
Dim types(params.Length - 1) As Type
Dim index As Integer
For index = 0 To params.Length - 1
types(index) = params(index).GetType
Next

While Not objectType Is Nothing
propertyInfo = objectType.GetProperty(name, flags, Nothing, Nothing,
types, Nothing)
If propertyInfo Is Nothing Then
objectType = objectType.BaseType
Else
Exit While
End If
End While

If propertyInfo Is Nothing Then
Throw New MissingMemberException(obj.GetType.FullName, "")
ElseIf Not propertyInfo.CanWrite Then
Throw New MissingMethodException(obj.GetType.FullName, "")
Else
propertyInfo.SetValue(obj, value, params)
End If
End Sub
/claes

"Alexander Walker" <al**@noemail.noemail> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...

Hello

I would like to write a method that allows me to pass a reference to an
instance
of a class, the name of a property of that class and a value to set that
property to, the method would then set the property of the instance to the
value

here is an example of what the method might look like

public void SetProperty(object instance, string property, object value)
{
//TODO: set the named property of the instance with the value
}

The method could be called like this

SetProperty(myClassInstance, "MyProperty", "My Value");

How would such a method be written? how do you access the members of an
instance
in a late bound manner, or is it even possible to pass a reference to the
actual
property like so

SetProperty(myClassInstance, myClassInstance.MyProperty, "The Value");

Where the method is not taking the value of myClassInstance.MyProperty but
the
reference to the actual property so that it can set its value, is this
possible?

Thanks

Alex



这篇关于按名称设置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆