简体   繁体   English

是否可以在不强制转换的情况下调用基础对象的方法?

[英]Can I call a method of a base object without casting it?

EDIT. 编辑。 I'm sorry for the typo. 对不起,我打错了。 Both objects are independent. 两个对象都是独立的。 OBJ1 is actually from a 3rd party and i have no access to its constructor so I built my own with some of its functionality and some of my own. OBJ1实际上来自第三方,我无法访问其构造函数,因此我使用其某些功能和我自己的功能构建了自己的。

I have a method which will be called using OBJ1 as a parameter. 我有一个将使用OBJ1作为参数调用的方法。 I have a need to write the exact same routine but passing OBJ2 as a paramater. 我需要编写完全相同的例程,但是将OBJ2作为参数传递。 OBJ1 and OBJ2 are not identical but they do have a property in common: Prop1. OBJ1和OBJ2不同,但是它们确实具有一个共同的属性:Prop1。

Details: OBJ1 is a 3rd party object with no public constructor so for testing, I created my own object which is "similar" but in no way identical to the original. 详细信息:OBJ1是没有公共构造函数的第三方对象,因此为了进行测试,我创建了自己的对象,该对象“相似”但与原始对象完全不同。 They have some properties in common including: Prop1. 它们具有一些共同的属性,包括:Prop1。

I'd like to do this: 我想这样做:

   private ObjectTypeA OBJ1 = new ObjectTypeA();
   private ObjectTypeB OBJ2 = new ObjectTypeB();
   main() 
   {
        GetPlacedOrderIDFromTag(OBJ1);
        GetPlacedOrderIDFromTag(OBJ2);

   }
   private void FunctionA (object _obj)
    {
        // I know this object has this property.  
         _obj.Prop1 = true;
    }

I tried implementing a partial interface like this: private partial interface IOrderUserTag { bool Prop1 { get; set; } } 我试过实现这样的部分接口: private partial interface IOrderUserTag { bool Prop1 { get; set; } } private partial interface IOrderUserTag { bool Prop1 { get; set; } }

but since OBJ1 is a 3rd party object, I cannot get the compiler to know that it adheres to this interface. 但是由于OBJ1是第3方对象,所以我无法让编译器知道它遵守该接口。

'Would love an idea for not having duplicate code I need to maintain. '我会喜欢一个没有重复的代码的想法,我需要维护。

Thanks, -Ed 谢谢,-Ed

C# does provide duck typing -- the dynamic keyword: C#确实提供了鸭子输入- dynamic关键字:

((dynamic)_obj).Prop1 = true;

That will work as long as _obj has a boolean setter called Prop1 (casing matters). 只要_obj有一个称为Prop1的布尔型设置器(大小写问题), Prop1

The other way to do it is to drop down to reflection level, it provides functions to search up and down the list of properties and you can call them with your own parameters. 另一种方法是下拉至反射级别,它提供了在属性列表中上下搜索的功能,您可以使用自己的参数进行调用。

Here's the Reflection way of doing it: 这是反射的方法:

    public void DoStuff(object obj)
    {
        PropertyInfo prop = obj.GetType().GetProperty("Prop1");

        if (prop != null && prop.PropertyType == typeof(bool) && prop.CanWrite)
        {
            prop.SetValue(obj, true);
        }
    }

You can put in whatever checks you feel necessary into the if statement. 您可以在if语句中放入您认为必要的任何检查。

Only if this applies, you just need to call same routines with different objects? 仅在这种情况下,您只需要使用不同的对象调用相同的例程?

public void FunctionA<T>(T obj)
{
  //obj.GetType().GetProperty("Prop1").SetValue(obj, true, null); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM