|
-
October 20th, 2008, 04:55 AM
#1
Newbie generics question
Hi all,
I'm using C# in .NET 3.5 to load xml files, defined by xsd schemas. I'm using LinQ to xsd which generates objects for me, using the xsd schemas. All my schemas have a root node which is a complex type containing a sequence of objects. e.g.
Code:
<xs:element name="ObjectList">
<xs:complexType>
<xs:sequence>
<xs:element ref="MyObject" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:element>
<xs:element name="MyObject">
<xs:complexType>
<xs:all>
<xs:element name="ID" type="xs:string" minOccurs="0"/>
</xs:all>
</xs:complexType>
</xs:element>
On compliation, the compiler generates an "ObjectList" class, which derives from XTypedElement. This contains a member, IList<MyObject> where MyObject also derives from XTypedElement. I can load the objects into memory easily using
Code:
var rootlist = ObjectList.Load(filepath);
IList<MyObject> = rootlist.MyObject;
Now what I want to do is to write a generic load function for all my objects (I have lots and lots of xsd schemas). I'm a bit stuck on how to do this. In C++ it's easy because you can create a list of XTypedElement pointers and initialise the list contents depending on what object type you are considering. However it's not good practice to use pointers in C# is it. I've had a look at generics but have come a bit unstuck. Does anyone have any tips on what sort of method I should be using?
Thanks in advance for your help.
-
October 20th, 2008, 05:05 AM
#2
Re: Newbie generics question
Code:
var rootlist = ObjectList.Load(filepath);
IList<MyObject> = rootlist.MyObject;
You are obviously talking about
Code:
var rootlist = ObjectList.Load(filepath);
IList<MyObject> myElements = rootlist.MyObject;
The myElements List will contain all your MyObject objects so whats the problem ? IList is already generic as you see so whats the problem ? If you have for example objects of customers and objects of products and other very different objects you cannot do other then building different lists for each of them like
Code:
IList<Customer> customers= rootlist.Customers;
IList<Product> product = rootlist.products;
This cannot be simplified any more IMHO
Last edited by JonnyPoet; October 20th, 2008 at 05:16 AM.
 Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
October 20th, 2008, 05:08 AM
#3
Re: Newbie generics question
Yeah, sorry, typo there. The problem is that I don't want to write a separate load function for each of my 40+ xsd files (and hence object types). I want to write one generic load function that returns a list of the relevant object types.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|