方法来处理对象与共同属性,但不同的对象类型 [英] Method to handle objects with properties in common, but different object types
问题描述
我收集了大量自动生成的对象。虽然他们都是不同的,不相关的类,所有的对象共享一些基本的属性(姓名,身份证等)。 我不控制这些对象的产生,所以很遗憾我不能把实现接口的理想方法。我想创造一个我通过这些对象中的任意一个的方法,做一些。使用这些公共属性。
一般的想法是这样的:
someObj中=新someObj中();
a.name =萨拉;
diffObj B =新diffObj();
b.name =乔;
串词=的String.Format(我与{0}和{1},
的getName(一)的getName(B));
私人字符串的getName(对象anyObjWithName)
{
返回anyObjWithName.name;
}
虽然自然这不起作用。
我想一个通用的方法可能持有的答案,但只有这样,我可以看到当前的类型是使用 genericMethod.Invoke叫它
,它仍然带有不能够解决的方法传递的对象的属性相同的问题。这不像调用一个泛型方法只有在执行时已知类型参数或如何调用通用方法用给定的类型的对象?的其中只有类型或类型的属性,在该方法中使用的,相对于对象的属性。
我知道,这将是(很)容易出现错误,但我可以保证,遇到的所有对象都会有共同的属性被操纵。
我可以保证,遇到的将有共同属性的所有对象被操纵
块引用>
如果是这样的话,你可以使用
动态
:私人字符串的getName(动态anyObjWithName)
{
返回anyObjWithName.name;
}
请注意,使用的任何对象不具有<code >名称属性将不会失败,直到运行时。
如果您要添加安全的一点点,你可以赶上
RuntimeBinderException
如果属性不存在获取引发:私人字符串的getName(动态anyObjWithName)
{
尝试{
返回anyObjWithName.name;
}
赶上(RuntimeBinderException){
返回{}未知;
}
}
I have a large collection of automatically generated objects. Although they are all of different, non-related classes, all of the objects share some basic properties (name, id, etc.). I do not control the generation of these objects, so unfortunately I cannot take the ideal approach of implementing an interface. I would like to create a method in which I pass an arbitrary one of these objects and do something using these common properties.
The general idea would be something like:
someObj a = new someObj(); a.name = "sara"; diffObj b = new diffObj(); b.name = "joe"; string phrase = string.Format("I am with {0} and {1}", getName(a), getName(b)); private string getName(object anyObjWithName) { return anyObjWithName.name; }
though naturally this does not work.
I thought a generic method might hold the answer, but the only way I can see to call it with the current type is using
genericMethod.Invoke
, which still carries the same issue of not being able to resolve the properties of the passed object in the method. This is unlike Calling generic method with a type argument known only at execution time or How to call generic method with a given Type object? where only the type, or properties of the type, are used in the method, as opposed to properties of the object.I am aware that this would be (very) prone to error, but I can guarantee that all objects encountered will have the common properties being manipulated.
解决方案I can guarantee that all objects encountered will have the common properties being manipulated
If that's the case, you can use
dynamic
:private string getName(dynamic anyObjWithName) { return anyObjWithName.name; }
Be aware that using any object that does not have a
name
property will not fail until run-time.If you want to add a little bit of safety you can catch the
RuntimeBinderException
that gets thrown if the property does not exist:private string getName(dynamic anyObjWithName) { try { return anyObjWithName.name; } catch(RuntimeBinderException) { return "{unknown}"; } }
这篇关于方法来处理对象与共同属性,但不同的对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!