Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Let's say i have a class that is derived from PropertyDescriptor

 

public class MyProp : PropertyDescriptor
{
   //implement all abstract methods      
}

 

why would something like the following return an undefined value or need to be cast

in the first place since MyProp "is a" PropertyDescriptor and PropertyDescriptorCollections contains a collection of PropertyDescriptors

public class SomeClass
{
       private int id=0;
       private string customer="";
       
       public SomeClass()
       {
             foreach(PropertyDescriptor p in TypeDescriptor.GetProperties(this.GetType()))
             {
                      //MyProp mP = p;//this won't compile type conversion
                        MyProp mP = p;//mp is undefined
                        PropertyDescriptor prD = p//this works
                      
             }
       }

       public int ID{get{return id;}set{id=value;}}
       public string Customer{get{return customer;}set{customer=value;}}

}

 

if MyProp is a PropertyDescriptor how can this be happening. Any help would be greatly appreciated.

  • Administrators
Posted

The "is a" only works one way round, i.e. MyProp is a PropertyDescriptor but there no guarentee than a PropertyDescriptor is a MyProp.

 

In other words you can always safetly assign a MyProp object to a variable of type PropertyDescriptor because that is always going to be a safe assignment, there is no way the compiler can verify that the reverse conversion will be true - hence the cast is required.

 

try using

foreach(PropertyDescriptor p in TypeDescriptor.GetProperties(this.GetType()))
{
MyProp mP =(MyProp)  p;
}

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...