abstract class Base
{
};
class A : Base
{
}
class B: Base
{
}
class C
{
public virtual Base BaseClass {get; set;}
}
If you now list all C objects from database and try to check of what type is C.BaseClass with
if (c.BaseClass is A)
{
}
else if (c.BaseClass is B)
{
}
else
{
//ERROR
}
you will get an error.
Simplest way to solve this problem is to add method in base class that will returns instance to itself:
abstract class Base
{
public virtual Base This()
{
return this;
}
};
Now, you can check:
if (c.BaseClass.This() is A)
{
}
else if (c.BaseClass.This() is B)
{
}
2 comments:
Your post saved me a bunch of time spent in a crunch situation :-) Thanks!
This works!!! Thanks!
Post a Comment