473,406 Members | 2,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Generics and their resulting class. Possible to obtain?

Hi,
We need to serialize (binary) objects containing generic collections. The
thing is, when we try to get the objects back (deserialize) with a different
instance of the application, we receive an exception stating the constructor
of the generic class does not exist. So Is there a way to obtain the
resulting classes of the generics we use so we can add them as "normal" code
in our app? We can develop our own starting from non-generic collections,
but then we would have to develop our own methods and maintain them. The
generics are already debuged and maintained by MS so we would like to
generate them with the same generator the framework generates them if
possible.

So if I summarize what we want : We want to take the resulting class from a
given generic and "copy-paste" the code and create a "user class" that we
will add to our project.

Is it possible?

Thanks

ThunderMusic
Aug 14 '07 #1
8 1073
On Aug 14, 7:11 am, "ThunderMusic"
<NoSpAmdanlatathotmaildot...@NoSpAm.comwrote:
Hi,
We need to serialize (binary) objects containing generic collections. The
thing is, when we try to get the objects back (deserialize) with a different
instance of the application, we receive an exception stating the constructor
of the generic class does not exist.
So there's your starting point. Do you have a constructor for the
generic class?
So Is there a way to obtain the
resulting classes of the generics we use so we can add them as "normal" code
in our app?
What is "normal"?
We can develop our own starting from non-generic collections,
but then we would have to develop our own methods and maintain them. The
generics are already debuged and maintained by MS so we would like to
generate them with the same generator the framework generates them if
possible.
Nothing is as it seems. MS code is not bug free. A while ago, a guy
had a problem here because he was using a structure rather than a
class--and precisely because of this, he had a problem (mixing
structures with classes is like mixing trucks with automobiles--they
don't really mix. Avoid structures).
>
So if I summarize what we want : We want to take the resulting class from a
given generic and "copy-paste" the code and create a "user class" that we
will add to our project.

Is it possible?
No. Not as you described it. It might help to know what interface
you're using.

Good luck,

RL

Aug 14 '07 #2
On Aug 14, 3:31 pm, raylopez99 <raylope...@yahoo.comwrote:

<snip>
Nothing is as it seems. MS code is not bug free. A while ago, a guy
had a problem here because he was using a structure rather than a
class--and precisely because of this, he had a problem (mixing
structures with classes is like mixing trucks with automobiles--they
don't really mix. Avoid structures).
Structures and classes mix absolutely fine - you just need to know
what's going on. If you don't understand how each of them works, then
indeed you'll get problems.

Jon

Aug 14 '07 #3
hi,

ok... maybe I explained it wrong... let's take Dictionary<int, string>.
When I instanciate one of those, .NET generates on-the-fly a new class for
Dictionary<int, string(or maybe it's at compile time, but the problem
stays the same for us because it's a web app, so it can be recompiled at any
time). If I serialize it, it works fine. If I deserialize it within the same
app instance, it works fine. If a new instance is started and I try to
deserialize it, then I get an exception stating there is no constructor for
Dictionary<int, string>.

So, what I want to know is if it is possible to get the resulting class (The
one .NET generates from Dictionary<int, string>) so we can take the code and
put it in a file in our project so the class always stays the exact same
class?

btw, I know MS code is not bug free, but if we can build on something that
was tested by thousands if not millions of users (developers), we go for it.

Thanks

ThunderMusic
"raylopez99" <ra********@yahoo.comwrote in message
news:11**********************@k79g2000hse.googlegr oups.com...
On Aug 14, 7:11 am, "ThunderMusic"
<NoSpAmdanlatathotmaildot...@NoSpAm.comwrote:
>Hi,
We need to serialize (binary) objects containing generic collections. The
thing is, when we try to get the objects back (deserialize) with a
different
instance of the application, we receive an exception stating the
constructor
of the generic class does not exist.

So there's your starting point. Do you have a constructor for the
generic class?
>So Is there a way to obtain the
resulting classes of the generics we use so we can add them as "normal"
code
in our app?

What is "normal"?
>We can develop our own starting from non-generic collections,
but then we would have to develop our own methods and maintain them. The
generics are already debuged and maintained by MS so we would like to
generate them with the same generator the framework generates them if
possible.

Nothing is as it seems. MS code is not bug free. A while ago, a guy
had a problem here because he was using a structure rather than a
class--and precisely because of this, he had a problem (mixing
structures with classes is like mixing trucks with automobiles--they
don't really mix. Avoid structures).
>>
So if I summarize what we want : We want to take the resulting class from
a
given generic and "copy-paste" the code and create a "user class" that we
will add to our project.

Is it possible?

No. Not as you described it. It might help to know what interface
you're using.

Good luck,

RL

Aug 14 '07 #4
Jon Skeet [C# MVP] wrote:
On Aug 14, 3:31 pm, raylopez99 <raylope...@yahoo.comwrote:

<snip>
>Nothing is as it seems. MS code is not bug free. A while ago, a guy
had a problem here because he was using a structure rather than a
class--and precisely because of this, he had a problem (mixing
structures with classes is like mixing trucks with automobiles--they
don't really mix. Avoid structures).

Structures and classes mix absolutely fine - you just need to know
what's going on. If you don't understand how each of them works, then
indeed you'll get problems.

Jon
I send that! Structs are indeed incredibly handy items. They are great
for abstracting something that you might normally pass around as (say)
an int. A lot of times I'll create a struct for some business
concept--like a SKU--instead of just passing around an int. This is
incredibly handy for a lot of reasons.

I'm not quite sure how you would "mix" structs and classes anyhow...

--
-glenn-
Aug 14 '07 #5
On Aug 14, 3:52 pm, GlennDoten <gdo...@gmail.comwrote:

<snip>
I'm not quite sure how you would "mix" structs and classes anyhow...
I suspect Ray means in terms of putting a reference type variable as a
member of a struct. It's rarely useful, but it *does* work, and so
long as you understand what reference types and value types actually
do, there shouldn't be any surprises.

Jon

Aug 14 '07 #6
ThunderMusic,

If you are serializing a Dictionary<int, stringto a byte stream, then
you should be able to recreate that instance in any instance of an app that
requires a Dictionary<int, string>. There should be no errors here.

Can you recreate the example? I've done it here with that type, and
have no problems serializing/deserializing a dictionary like that and then
recreating the instance in another run of the app.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"ThunderMusic" <No*************************@NoSpAm.comwrote in message
news:eA**************@TK2MSFTNGP06.phx.gbl...
hi,

ok... maybe I explained it wrong... let's take Dictionary<int, string>.
When I instanciate one of those, .NET generates on-the-fly a new class for
Dictionary<int, string(or maybe it's at compile time, but the problem
stays the same for us because it's a web app, so it can be recompiled at
any time). If I serialize it, it works fine. If I deserialize it within
the same app instance, it works fine. If a new instance is started and I
try to deserialize it, then I get an exception stating there is no
constructor for Dictionary<int, string>.

So, what I want to know is if it is possible to get the resulting class
(The one .NET generates from Dictionary<int, string>) so we can take the
code and put it in a file in our project so the class always stays the
exact same class?

btw, I know MS code is not bug free, but if we can build on something that
was tested by thousands if not millions of users (developers), we go for
it.

Thanks

ThunderMusic
"raylopez99" <ra********@yahoo.comwrote in message
news:11**********************@k79g2000hse.googlegr oups.com...
>On Aug 14, 7:11 am, "ThunderMusic"
<NoSpAmdanlatathotmaildot...@NoSpAm.comwrote:
>>Hi,
We need to serialize (binary) objects containing generic collections.
The
thing is, when we try to get the objects back (deserialize) with a
different
instance of the application, we receive an exception stating the
constructor
of the generic class does not exist.

So there's your starting point. Do you have a constructor for the
generic class?
>>So Is there a way to obtain the
resulting classes of the generics we use so we can add them as "normal"
code
in our app?

What is "normal"?
>>We can develop our own starting from non-generic collections,
but then we would have to develop our own methods and maintain them. The
generics are already debuged and maintained by MS so we would like to
generate them with the same generator the framework generates them if
possible.

Nothing is as it seems. MS code is not bug free. A while ago, a guy
had a problem here because he was using a structure rather than a
class--and precisely because of this, he had a problem (mixing
structures with classes is like mixing trucks with automobiles--they
don't really mix. Avoid structures).
>>>
So if I summarize what we want : We want to take the resulting class
from a
given generic and "copy-paste" the code and create a "user class" that
we
will add to our project.

Is it possible?

No. Not as you described it. It might help to know what interface
you're using.

Good luck,

RL


Aug 14 '07 #7
We need to serialize (binary) objects containing generic collections. The
thing is, when we try to get the objects back (deserialize) with a
different instance of the application, we receive an exception stating the
constructor of the generic class does not exist.
Why is this happening? I am able to serialize (binary) a bunch of classes
into a file and then I am able to deserialize them at any time on different
instances of the application. Probably not the exact same thing you are
doing but equivalent.

What else do you have going on?
Aug 14 '07 #8
Thanks to everyone of you.
We finally found the problem. We read the exception stack wrong and thought
it was the actual System.Collections.Generics.Dictionary that was not
deserializable. The thing is, we made a custom Dictionary (that inherits
from Dictionary) that can be serialized in XML and it's called Dictionary
just the same, but in another namespace... The "Serialization" constructor
(the 7th constructor of Dictionary<>) was missing, so that's why we had this
exception. Now everything is back on track.

Thanks and sorry for the waste of time

ThunderMusic

"Rene" <a@b.comwrote in message
news:uu**************@TK2MSFTNGP04.phx.gbl...
>We need to serialize (binary) objects containing generic collections. The
thing is, when we try to get the objects back (deserialize) with a
different instance of the application, we receive an exception stating
the constructor of the generic class does not exist.

Why is this happening? I am able to serialize (binary) a bunch of classes
into a file and then I am able to deserialize them at any time on
different instances of the application. Probably not the exact same thing
you are doing but equivalent.

What else do you have going on?


Aug 14 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: andrew queisser | last post by:
I've read some material on the upcoming Generics for C#. I've seen two types of syntax used for constraints: - direct specification of the interface in the angle brackets - where clauses I...
23
by: Luc Vaillant | last post by:
I need to initialise a typed parameter depending of its type in a generic class. I have tried to use the C++ template form as follow, but it doesn't work. It seems to be a limitation of generics...
12
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
1
by: Ole Nielsby | last post by:
I'm puzzled at how static fields intersect with generics, as the following snippet illustrates. namespace MonkeyParty { abstract class Fruit { } class Apple : Fruit { } class Pear : Fruit {...
4
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
7
by: SpotNet | last post by:
Hello NewsGroup, Reading up on Generics in the .NET Framework 2.0 using C# 2005 (SP1), I have a question on the application of Generics. Knowingly, Generic classes are contained in the...
7
by: Andy Bell | last post by:
Can this be done via .net generics? How? The % signs below are just to show how I want to do it, I realise they're not valid syntax. public abstract class BaseSelectionRequirement { ......
8
by: ThunderMusic | last post by:
Hi, We need to serialize (binary) objects containing generic collections. The thing is, when we try to get the objects back (deserialize) with a different instance of the application, we receive...
6
by: =?Utf-8?B?UXVhbiBOZ3V5ZW4=?= | last post by:
I am trying to create a generics class with multiple constrains, as follows: public class KeyHandler<Twhere T : TextBoxBase, ComboBox When I try that, the compiler would complain: The class...
1
by: JosAH | last post by:
Greetings, Introduction This week I'll write a bit about generics (those funny angular brackets). I need an example and decided to use sets and some of their operations. This weeks' article...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.