|
If you read the text and not the code the solution is in there. Reflection and recursion.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
I've not tested it but you might be able to modify the following code; this basically does a deep copy of an object using serialization. Some minor modifications should allow to to write to a FileStream rather than a MemoryStream and then you'd extrapolate two functions (get and set).
<br />
public static T DeepCopy<t>(T obj)<br />
{<br />
object result = null;<br />
<br />
using (var ms = new MemoryStream())<br />
{<br />
var formatter = new BinaryFormatter();<br />
formatter.Serialize(ms, obj);<br />
ms.Position = 0;<br />
<br />
result = (T)formatter.Deserialize(ms);<br />
ms.Close();<br />
}<br />
<br />
return (T)result;<br />
}<br />
<br />
</t>
I haven't got time to work through the solution fully so I've left it as an exercise for you.
It definitely isn't definatley
modified on Thursday, January 22, 2009 6:18 AM
|
|
|
|
|
Thanks for your code but the line
formatter.Serialize(ms, obj);
gives the following exception. I marked the class as [Serializable].
Type 'System.Windows.Forms.Form' in Assembly 'System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.
|
|
|
|
|
Hi All,
Actually I've coded a small windows application which takes up a dll and then using reflection finds out the various classes and properties in it (just like an object browser ) and initializes them at run time. I've used recursion to do this for initializing inner classes. But for a large dll which has many innerclasses and properties the application is throwing 'Stack Overflow exception'.
Is there any way to avoid it ? or the alternate way instead of using recursion?
"Don't worry if it doesn't work right. If everything did, you'd be out of a job." (Mosher's Law of Software Engineering)
|
|
|
|
|
If you're getting a stack overflow then, chances are, you have gone into an infinite loop into your recursion. You may need to trace through (tedious I know), and identify where the loop is kicked off - it could be down to a circular dependency going on in there.
|
|
|
|
|
If your recursive function accepts a lot of parameters, or even just one or two string parameters, you can overflow the stack rather quickly. Back in the day, the stack was only 64K big, but I don't know if that's still the case, or if the stack size can be increased. Each function call requires a certain amount of overhead, and having a recursive function that tacks strings requires as much stack space as the string(2) requires in memory (in addition to the regular overhead needed by the function call itself).
If it were me, I would look for a solution that didn't require recursion (if possible), or abstract out the recursion so that you don't have to make so many recursive calls before exiting the loop.
.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
John Simmons / outlaw programmer wrote: the stack was only 64K big, but I don't know if that's still the case, or if the stack size can be increased.
The default reserved stack size is 1 MB. You can change it for any executable using editbin.exe /STACK
|
|
|
|
|
Imagine that there might be an extreme case where there is a class in a class in a class in a class in a class in a class in a class in a class in a class in a class in a class in a class in a class in a class in a class in a class, but that is still very very far from causing a stack overflow. If your method has a huge amount of local variables, you might use a few kilobytes out of the megabyte of stack space that is available.
So, there has to be something wrong with your recursion...
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Hi,
I think this is the issue that is causing trouble...but i don't find any other way barring recursion which can easily navigate into the inner classes...The dll's in fact contain many number of innerclasses and properties... so is there any solution for this??
"Don't worry if it doesn't work right. If everything did, you'd be out of a job." (Mosher's Law of Software Engineering)
|
|
|
|
|
Spunky Coder wrote: I think this is the issue that is causing trouble...
I very much doubt that....
Spunky Coder wrote: The dll's in fact contain many number of innerclasses and properties...
I have several times used a class in a class, perhaps sometime a class in a class in a class. I could imagine a reason to do perhaps four or five levels, but hardly more than that. Do you say that the assembly contains classes that are nested inside each other to something like hundred levels?
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
There is probably something going wrong in the recursion as the nesting should not really get that deep. When the exception occurs take a second to look at the stack trace in the debugger and get an idea of how deep the recursion got. However, to answer your question the alternate of recursion is to use and manage a Stack yourself. Using the Generic Collections from .NET 2.0, you can use Stack<T> for this purpose. It's really the same concept as recursion because in recursion you're using the call stack and the system is managing it for you. Anyhow, there are bunches of good articles on the internet of how to mimic recursion with a stack; off-hand I found this one:
http://haacked.com/archive/2007/03/04/Replacing_Recursion_With_a_Stack.aspx[^]
Hope that helps!
Keep It Simple Stupid! (KISS)
|
|
|
|
|
Hi,
Thanks for the reply...I have gone through the article but seems that it doesn't work if there are any circular references. But in the dll's i use, circular reference exist. So is this kind of situation can be solved ??
Also is it a good thing to increase the stack size ??
"Don't worry if it doesn't work right. If everything did, you'd be out of a job." (Mosher's Law of Software Engineering)
|
|
|
|
|
Spunky Coder wrote: But in the dll's i use, circular reference exist.
Class definitions can not be circular. A class can not be defined inside itself...
Spunky Coder wrote: Also is it a good thing to increase the stack size ?
You have something around a megabyte of stack space, what you are doing should typically use something like 0.1% of that. If you run out of stack space, it's not the size of the stack that is the problem.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
I generally follow this pattern to prevent circular references. Create a List<t></t> that will accompany your stack. This will contain the items that have already been processed. At the top of your stack loop, the first step is to pull the next item of the stack and the second step should be to check that item against the list of items already processed. If it has already been processed, discard it and go to the next item. I generally put this in place even when the data SHOULDN'T have circular references, but I prefer to be safe rather than deal with some difficult to find bug later on.
Hope in one hand and poop in the other; see which fills up first. Hope and change were good slogans, now show us more than words.
|
|
|
|
|
|
Tuntgerhuu wrote: Help me
We all are with you. Go on, friend.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Hi,
I've created WCF application, from WCF client application i'm calling service application (basically Windows Service) methods but i'm not able to receive service response there.
If I call from multi thread environment then I'm getting response, what should be done for NON-Multi threading call?
Thanks
|
|
|
|
|
I can use String.IsNullorEmpty() to test a string for null. How can I test a string for DBnull please?
|
|
|
|
|
Hi,
you can use
<br />
if(myValue != DBNull.Value) {<br />
...<br />
<br />
}<br />
for the check if the value equals the value DBNull.
Regards
Sebastian
|
|
|
|
|
Just an addition, if you are using a DataReader you can call the method
<br />
DataReader myDataReader = null;<br />
...<br />
if(myDataReader.IsDBNull(...)) {<br />
...<br />
}<br />
|
|
|
|
|
Hi,
This is from Chandrakanth.
My question is How to Convert Data from Tiff file to Text Format.
Actually we are using OCR(Third Party Tool) for this conversion. But we
don't want to use that Third party tool. We do want get the data with
out using OCR. Is there any other way to get the Data from Tiff File to
Text Format.
Actually i am looking for some souce code.
Can any one give me reply for this
Thanks and Regards
Chandrakanth
|
|
|
|
|
Hi,
as far as I know is a Tiff File an Image file. That means that the bytes within the file just contains information about which color has the pixel at position x and y. The data itself doesn't know anything about if it is text or the picture of something.
So if you really don't want to use a third party tool, "all" you have to do is to write yourself an OCR application.
Regards
Sebastian
|
|
|
|
|
Hello ! I'm new to C# and I've found in one sample from Codeproject something like this:
string temp=@"temp string blah blah";
What does that @ before string ?
Thanks
|
|
|
|
|
It means verbatim string literal[^], so special characters don't need to be escaped.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
Wow, that was fast
Thanks, I understand now
|
|
|
|