Drop one object to another type

I have one class named A and one class B

public class A : UserControl { }

public class B : UserControl { }

Now I have one assembly whose class function accepts objects of class A. This assembly is not created by me, so I have no control. This is mainly an assembly of third-party manufacturers.

But I want to provide my objects of class B, since it is customized. Be sure that it contains all the properties of class A. How can I give my class B object to type A so that I can integrate a third-party assembly into my project, as well as customize the look to suit my needs?

If I feel like something (A)objB, this is unacceptable. Then I tried this:

UserControl control = objB as UserControl;

A objA = control as A;

But the problem in this case is objA null.

To avoid confusion: Class A and assembly are provided by a third party.

Thanks in advance:)

+5
6

, . ( Dog : Animal Cat : Animal):

public static explicit operator A(B b) {
    // code to populate a new instance of A from b
}

-

public static void PropertyCopyTo<TSource, TDesination>(
    this TSource source,
    TDestination destination
) {
    // details elided
}

// b is B
// a is A
b.PropertyCopyTo<A>(a);

b a.

+4

B, A, B A. B A, A.

+2
+1

B A ( B is A) , , A. , # (.. ).

0
source

You can inherit from class A:

public class B : A {
}

And if you need to override some methods / properties, just set them in virtualin class A.

-1
source

I think you really cannot do this.

Why not add your properties to the partial class A?

-1
source

All Articles