1

Final solution:

public class CustomOptionMetadata : OptionMetadata
{
    public CustomOptionMetadata(OptionMetadata optionMetadata) : base() 
    {
        this.Description = optionMetadata.Description;
        this.ExtensionData = optionMetadata.ExtensionData;
        this.Managed = optionMetadata.IsManaged;
        this.Label = optionMetadata.Label;
        this.MetadataId = optionMetadata.MetadataId;
        this.Value = optionMetadata.Value;
    }

    public string MappedName { get; set; }
    public bool? Managed { get; set; }
}

.

foreach (MappingEntity meAtt in mappingEntityList.FindAll(me => me.ParentID == meEnt.ID))
{
    CRMOrganizationEntityAttribute crmOrganizationEntityAttribute = new CRMOrganizationEntityAttribute(meAtt.ID, meAtt.Name, true);
    if (meAtt.IsPicklist)
    {
        List<OptionMetadata> optionMetadataList = GetAttributePickList(meEnt.Name, meAtt.Name);
        if (optionMetadataList.Count > 0)
        {
            crmOrganizationEntityAttribute.OptionMetadataColl = optionMetadataList.Select<OptionMetadata, CustomOptionMetadata>(metadata => new CustomOptionMetadata(metadata)).ToList();
        }
        else
        {

        }
    }

    CRMOrgEnt.CRMAttributeNameList.Add(crmOrganizationEntityAttribute);
}

============================================================================

I am currently working on a mapping application, which already has a lot of custom objects. In an attempt to limit the lookups and definition of the classes, I thought about extending the already existing classes which I need anyways.

The OptionMetaDataCollection is part of the Microsoft.Xrm.SDK.Metadata library and has the OptionMetaData object in its Ilist.

Now I would like to create something like this:

public partial class CustomOptionMetadataCollection : OptionMetadataCollection
{
    public CustomOptionMetadataCollection() : base() { }
}

public partial class CustomOptionMetadata : OptionMetadata
{
    public CustomOptionMetadata() : base() { }

    public string MappedName { get; set; }
}

But how would I change the Ilist<OptionMetaData> which is currently in the OptionMetadatacollection to my IList<CustomOptionMetaData>. I am aware that I can make my own Ilist of the CustomOptionMetaDataCollection, however I would (with my mind on the rest of the code) list to use something simulair with more advanced collection classes.

I am also wondering how I would than map the OptionMetaDataCollection object to my CustomOptionMetadataCollection. I can create a loop but I am looking for a more effecient way to do the mapping.

Any help is much appreciated.

3
  • Is your question how do you initialize the custom classes from the Microsoft defined ones? Also, I'm not understanding what benefit you're getting from the custom classes...
    – Daryl
    Jan 15, 2013 at 22:05
  • Please see below in the comment of the answer. Thank you for your time. Jan 16, 2013 at 4:35
  • I would use a Where, not FindAll. A find all will create a new list, copying all the values, the Where will just enumerate over your current list.
    – Daryl
    Jan 16, 2013 at 13:40

1 Answer 1

3

If you want to convert an IList<OptionMetadata> to a IList<CustomOptionMetadata>, you're effectively asking; How can I convert my base class (OptionMetadata) to a derived class (CustomOptionMetadata)?

Let's take the IList out of the equation for the moment - after all its just holding multiple copies of the class.

At it simplest level you basically want to do:

OptionMetadata optionMetadata = new OptionMetadata();
CustomOptionMetadata customOptionMetadata = (CustomOptionMetadata)optionMetadata;

Unfortunately this type of downcasting isnt really something C# does.

As Adam Wright describes in A way of casting a base type to a derived type.

This is downcasting, and there is no sane down way to handle it, . . . (subclasses provide more than base classes - where does this "more" come from?)

So that said there are other ways to handle this, for example adding a constructor to your custom class which takes the base class.

class OptionMetadata
{
    public int X;
}

class CustomOptionMetadata : OptionMetadata
{
    public int Y;

    public CustomOptionMetadata(OptionMetadata metadata)
    {
        this.Y = metadata.X * 2;
    }
}

Then at its simplest it becomes:

OptionMetadata optionMetadata = new OptionMetadata();
CustomOptionMetadata customOptionMetadata = new CustomOptionMetadata(optionMetadata);

And to put the IList back into context.

IList<OptionMetadata> optionMetadata = new List<OptionMetadata>() 
{            
    new OptionMetadata(),
    new OptionMetadata()
};

IList<CustomOptionMetadata> customOptionMetadata = optionMetadata
    .Select<OptionMetadata, CustomOptionMetadata>(metadata => new CustomOptionMetadata(metadata)).ToList();

I'm not really sure what you mean by 'mapping' one collection to another, but its sounds a bit like the same problem where you are trying to downcast a base class to a derived class. It not clear from your question what you are trying to achieve (what is the derived class actually doing?). If you just want to copy the contents of one collection to another I would suggest having a look at How do I copy items from list to list without foreach?.

3
  • Thank you for your help. I needed to know exactly what you answered. I was wondering if this type of downcasting worth be supported. I also wondered if the collection is more than just an Ilist and therefor If I can / should replace the object within that Ilist of the collection definition. But your answer is clear, I will create a Ilist. Also: .Select<OptionMetadata, CustomOptionMetadata>(metadata => new CustomOptionMetadata(metadata)).ToList(); is what I meant with mapping. I do not want to loop one list to fill the other and this is exactly how I want to resolve this. Thank you very much Jan 16, 2013 at 4:30
  • @Hendricks you do realize that underneath the covers, the select is just dong a loop?
    – Daryl
    Jan 16, 2013 at 11:57
  • @Daryl, Yes I do but I am trying to learn some while doing the development and I knew there is a "more clean" way for doing this than writing your own foreach loop. So this was part of what I was looking for. Thank you Daryl. Any other tips or points on for example my final solution are always much appreciated. Jan 16, 2013 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.