简体   繁体   English

在派生类中键入复杂对象的类型

[英]Type casting of complex object in derived class

I have bit complex problem. 我有点复杂的问题。 Rather its strange but its requirement. 相反它的奇怪但它的要求。 I have two different namespaces and both of them have same class structure. 我有两个不同的命名空间,它们都有相同的类结构。 Class1 of namespace2 inherits class1 of namespace1. namespace2的class1继承namespace1的class1。 Each parameter of class are of other class type and each type is in respective namespace ie Type11 is in namespace 1 and Type21 is in namesapce 2(First digit denotes the namespace). 类的每个参数都是其他类类型,每个类型都在各自的名称空间中,即Type11在名称空间1中,Type21在namesapce 2中(第一个数字表示名称空间)。 Now in derived class constructor, I need to inherit to base class constructor. 现在在派生类构造函数中,我需要继承到基类构造函数。 To do this, I need to type cast each object to its base class. 为此,我需要将每个对象的类型转换为其基类。 But when I typecast the object. 但是当我对该对象进行类型转换时。 it shows error like 'cannot convert namespace1.Type21 to namespace2.Type11' 它显示错误,如'无法将namespace1.Type21转换为namespace2.Type11'

Note: namespace1 and namespace2 both are different class library projects.I am adding referance of namesapce1 project in namespace2 project. 注意: namespace1和namespace2都是不同的类库项目。我在namespace2项目中添加了namesapce1项目的referance。 Type11, Type21 etc are class itself. Type11,Type21等本身就是类。

Here each type(ie Type11, Type12 etc.) is again class which having method parameters and properties of some other complex type. 这里每个类型(即Type11,Type12等)都是具有方法参数和其他复杂类型属性的类。

Here is my sample code. 这是我的示例代码。 I have tried to replicate the same scenario as I am facing in real. 我试图复制与我所面对的相同的场景。

namespace Namespace1
{
    public class TestClass1
    {
        private Type14 obj4;
        public Type15 obj5;
        public TestClass1()
        { 
        }

        public TestClass1(Type11 obj1)
        {
        }

        public TestClass1(Type11 obj1,Type12 obj2)
        {
        }

        public TestClass1(Type11 obj1,Type12 obj2,params Type13[] obj3) 
        {
        }
    }
}

namespace Namespace2
{
    public class TestClass1:Namespace1.TestClass1
    {
        private Type14 obj4;
        public Type15 obj5;

        public TestClass1():base()
        {  
        }

        public TestClass1(Type21 obj1):base((namespace1.Type11)obj1)
        {
        }

        public TestClass1(Type21 obj1,Type22 obj2):base((namespace1.Type11)obj1,(namespace1.Type12)obj2)
        {
        }

        public TestClass1(Type21 obj1,Type22 obj2,params Type23[] obj3):base((namespace1.Type11)obj1,(namespace1.Type12)obj2,(namespace1.Type13)obj3)
        {
        }
    }
}

Can anyone please tell me how can I do type casting in this case? 谁能告诉我在这种情况下如何进行打字?

From what I gather Type11 and Type21 are not part of the same type hierarchy (ie, neither of them is a base class of the other). 从我收集的Type11Type21不是同一类型层次结构的一部分(即,它们都不是另一个的基类)。

There is no "default" way to type cast in this case. 在这种情况下,没有“默认”方式来输入类型。 You have two options: 您有两种选择:

  1. Write converters for the types and call these to convert. 为类型写入转换器并调用它们进行转换。 This is the only way if you cannot change the source code of the types. 如果您无法更改类型的源代码,这是唯一的方法。
  2. Write conversion operators for the types, if you have access to the source code. 如果您有权访问源代码,请为类型编写转换运算符。

Assuming you have access to the source code, an explicit conversion operator for the type can be written as follows: 假设您可以访问源代码,则可以按如下方式编写该类型的显式转换运算符:

// Assuming Type11 and Type21 have the same Foo property
// Add the following inside Type11
public static explicit operator Type21(Type11 t)
{
    return new Type21 { Foo = t.Foo };
}

You can then cast explicitly from Type11 to Type21: 然后,您可以从Type11明确地转换为Type21:

Type11 a = new Type11 { Foo = 123 };
Type21 b = (Type21)a;

You can even write an implicit casting operator by replacing explicit in the first example above with implicit , which enables you to assign without an explicit cast: 您甚至可以通过在上面的第一个示例中使用implicit替换explicit来编写隐式转换运算符,这使您可以在没有显式转换的情况下进行分配:

Type11 a = new Type11 { Foo = 123 };
Type21 b = a;

MSDN has more information on using conversion operators. MSDN有关于使用转换运算符的更多信息。

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

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