Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 55863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:26:39+00:00 2026-05-10T17:26:39+00:00

Scenario: I’m currently writing a layer to abstract 3 similar webservices into one useable

  • 0

Scenario:

I’m currently writing a layer to abstract 3 similar webservices into one useable class. Each webservice exposes a set of objects that share commonality. I have created a set of intermediary objects which exploit the commonality. However in my layer I need to convert between the web service objects and my objects.

I’ve used reflection to create the appropriate type at run time before I make the call to the web service like so:

    public static object[] CreateProperties(Type type, IProperty[] properties)     {         //Empty so return null         if (properties==null || properties.Length == 0)             return null;          //Check the type is allowed         CheckPropertyTypes('CreateProperties(Type,IProperty[])',type);          //Convert the array of intermediary IProperty objects into         // the passed service type e.g. Service1.Property         object[] result = new object[properties.Length];         for (int i = 0; i < properties.Length; i++)         {             IProperty fromProp = properties[i];             object toProp = ReflectionUtility.CreateInstance(type, null);             ServiceUtils.CopyProperties(fromProp, toProp);             result[i] = toProp;         }         return result;     } 

Here’s my calling code, from one of my service implementations:

Property[] props = (Property[])ObjectFactory.CreateProperties(typeof(Property), properties); _service.SetProperties(folderItem.Path, props); 

So each service exposes a different ‘Property’ object which I hide behind my own implementation of my IProperty interface.

The reflection code works in unit tests producing an array of objects whose elements are of the appropriate type. But the calling code fails:

System.InvalidCastException: Unable to cast object of type ‘System.Object[]’ to type ‘MyProject.Property[]

Any ideas?

I was under the impression that any cast from Object will work as long as the contained object is convertable?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T17:26:39+00:00Added an answer on May 10, 2026 at 5:26 pm

    Alternative answer: generics.

    public static T[] CreateProperties<T>(IProperty[] properties)     where T : class, new() {     //Empty so return null     if (properties==null || properties.Length == 0)         return null;      //Check the type is allowed     CheckPropertyTypes('CreateProperties(Type,IProperty[])',typeof(T));      //Convert the array of intermediary IProperty objects into     // the passed service type e.g. Service1.Property     T[] result = new T[properties.Length];     for (int i = 0; i < properties.Length; i++)     {         T[i] = new T();         ServiceUtils.CopyProperties(properties[i], t[i]);     }     return result; } 

    Then your calling code becomes:

    Property[] props = ObjectFactory.CreateProperties<Property>(properties); _service.SetProperties(folderItem.Path, props); 

    Much cleaner 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Scenario: I am writing a WCF client to access a Java/Metro webservice which requires
Scenario: There are multiple C structs, each of which contains a function pointer to
Scenario I’m writing a Chrome extension / userscript to add a little usability to
Scenario: A huge Patient class/entity with around 100 properties. But in the webpage I
Scenario: I am working on a Android project where in one particular openGL page,
Scenario : I'm currently trying to build a poor man's Google Maps using scripts
Scenario: An event is raised in class A that needs to be handled by
Scenario I have two wrappers around Microsoft Office, one for 2003 and one for
SCENARIO (iOS 5) I currently have a UITableView. It has 30 table rows The
Scenario I was reviewing one of my devs HTML structure for a project and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.