|
Hi all,
Does anyone have a nifty workaround for the fact that a class that uses XML Serialisation is required to have a default constructor?
I've got a class AuthorInfo, that has a readonly ID variable, that is set in the constructor:
//C# Class...
public class AuthorInfo
{
public readonly m_ID;
public AuthorInfo( uint ID )
{
m_ID = ID;
...
...
}
...
...
}
I'd like this class to be serialised to XML but as noted in MSDN:
"A class must have a default constructor to be serialized by XmlSerializer"
I don't really want to create a default constructor for this class, as I'd like to enforce the fact that it can't be created without an ID being supplied.
Can anyone think of a good way to do this? The only thing I can think of off the top of my head is to create an internal helper class that just deals with Serialisaton. That seems a bit messy to me, and would be awkward to use with Web Services (which is what I'm doing).
Any suggestions/comments appreciated...
TIA,
Pete
|
|
|
|
|
You could use a Static Constructor to initialise an internal static countID variable to zero, that just gets incremented and stored in a non-static member as a new ID in the default constructor. Other than that and what relationship the ID has to other data its a little difficult to say.
Quote from a clever bloke :
"I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein
|
|
|
|
|
Hi all,
in my C# app I have to call a function in a legacy Win32 DLL. This function fills a complex struct. Unfortunately, I don't know how to port the data structure to .NET. Here's a simple example:
public class Test
{
[StructLayout(LayoutKind.Sequential)]
public class Test
{
int i;
}
[StructLayout(LayoutKind.Sequential)]
public class Test2
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=10)]
public Test1[] tests;
}
public Test()
{
int size = Marshal.SizeOf(typeof(Test2));
}
} In the constructor, Marshal.SizeOf throws the exception Type Test2 can not be marshaled as an unmanaged structure; no meaningful size or offset can be computed.
How do I marshal fixed size arrays of struct properly?
Thanks in advance.
Regards
Thomas
Disclaimer: Because of heavy processing requirements, we are currently using some of your unused brain capacity for backup processing. Please ignore any hallucinations, voices or unusual dreams you may experience. Please avoid concentration-intensive tasks until further notice. Thank you.
|
|
|
|
|
First, those need to be struct s. Second, you may need to specify the Test1 struct size, not just the number of elements. If you expect a variable number of elements, you should look at the SizeParam(?) property of the MarshalAsAttribute to specify the index of a member that specifies the number of elements in the struct array.
Reminiscent of my younger years...
10 LOAD "SCISSORS"
20 RUN
|
|
|
|
|
Heath Stewart wrote:
First, those need to be structs.
Ok, but it doesn't help at the moment.
Heath Stewart wrote:
Second, you may need to specify the Test1 struct size, not just the number of elements. If you expect a variable number of elements, you should look at the SizeParam(?) property of the MarshalAsAttribute to specify the index of a member that specifies the number of elements in the struct array.
It's fixed sized, therefore I used SizeConst .
Thanks anyway.
Regards
Thomas
Disclaimer: Because of heavy processing requirements, we are currently using some of your unused brain capacity for backup processing. Please ignore any hallucinations, voices or unusual dreams you may experience. Please avoid concentration-intensive tasks until further notice. Thank you.
|
|
|
|
|
I had the same problem and the only way is to Marshal it as a byte[] array and not the encapsulating class. If the class has more members, things become a bit more difficult.
MyDUMeter: a .NET DUMeter clone
|
|
|
|
|
leppie wrote:
If the class has more members, things become a bit more difficult.
In fact, the structurs are cascaded about three levels
Regards
Thomas
Disclaimer: Because of heavy processing requirements, we are currently using some of your unused brain capacity for backup processing. Please ignore any hallucinations, voices or unusual dreams you may experience. Please avoid concentration-intensive tasks until further notice. Thank you.
|
|
|
|
|
I think it is time for a Custom Marshaller. You need to implement this interface.
public interface ICustomMarshaler
{
public void CleanUpManagedData(object ManagedObj);
public void CleanUpNativeData(IntPtr pNativeData);
public int GetNativeDataSize();
public IntPtr MarshalManagedToNative(object ManagedObj);
public object MarshalNativeToManaged(IntPtr pNativeData);
}
And set MarshalAs(UnmanagedType.CustomMarshaler) on the class. From there you should havenough control to marshal just about anything. Note: You can now make classes without Layout.
Cheers
MyDUMeter: a .NET DUMeter clone
|
|
|
|
|
leppie wrote:
I think it is time for a Custom Marshaller.
Ok, it's an opportunity to learn something new
BTW, I am not the only one with this problem: http://www.dotnet247.com/247reference/msgs/28/141537.aspx[^]
Regards
Thomas
Disclaimer: Because of heavy processing requirements, we are currently using some of your unused brain capacity for backup processing. Please ignore any hallucinations, voices or unusual dreams you may experience. Please avoid concentration-intensive tasks until further notice. Thank you.
|
|
|
|
|
Hi,
This is my followup to my previous question. Even after I choose to 'End Process'/'End Process Tree' of aspnet_wp.exe, because it is taking too much of resources, it keeps spawning a new instance, and even after ensuring that no HTTP requests are being sent. I have closed all browsers and all VS.NET windows.
And this instance is taking too much of memory too.
What is the problem?
Deepak Kumar Vasudevan
http://deepak.portland.co.uk/
|
|
|
|
|
Deepak Kumar Vasudevan wrote:
Even after I choose to 'End Process'/'End Process Tree' of aspnet_wp.exe, because it is taking too much of resources, it keeps spawning a new instance, and even after ensuring that no HTTP requests are being sent. I have closed all browsers and all VS.NET windows.
This is default behaviour. In case one application crashes another.
Deepak Kumar Vasudevan wrote:
And this instance is taking too much of memory too.
It should not be. ASP.NET is stateless in nature. WHy is it taking so much memory? Can you not unload some memory?
MyDUMeter: a .NET DUMeter clone
|
|
|
|
|
Hi,
How can we control in WindowsXP / Windows 2000 Server how much memory each process is taking. If that is the case, can we restrict the amount of memory that is allocated to ASP.NET Worker Process (aspnet_wp.exe)
Deepak Kumar Vasudevan
http://deepak.portland.co.uk/
|
|
|
|
|
Hi all,
I have the following problem, that I am facing. When my ASP.NET page loads and after the page load has completed and even after the browser IE spinner stops spinning, when I check Processes tab in Task Manager, aspnet_wp.exe is still consuming heavy memory and CPU usage is more than 90%. aspnet_wp.exe does not get stopped after page_load is complete.
And very frequently my EventViewer gets so many error messages of the type:
(*) aspnet_wp.exe (PID: 216) was recycled because it was suspected to be in a deadlocked state. It did not send any responses for pending requests in the last 180 seconds.
Why is this happening? I am currently using C#-based ASP.NET application and my backend is IBM DB2 running on RS/6000 AIX server. I am using OLEDB Provider given by IBM DB2 to connect to the database using System.Data.OleDb.
We also frequently get 'Server Application Unavailable' and the following message gets recorded onto the database:
aspnet_wp.exe (PID: 668) stopped unexpectedly.
Please help...
Deepak Kumar Vasudevan
http://deepak.portland.co.uk/
|
|
|
|
|
Hi all,
I would like to have the following clarifications. With C#, writing to EventLog and Reading is bit more easier. But could somebody advise me regarding the same.
- Any standards that need to be followed, while writing to System EventLogs.
- What kind of data can be written into eventlogs?
- What is the normal industry custom to adopt when writing to the EventLog also fails? How should the application respond to the user in this circumstance?
Deepak Kumar Vasudevan
http://deepak.portland.co.uk/
|
|
|
|
|
I'm working with PerformanceCounter and I'm noticing very strange behaviour.
I can't add any instances to my counters once instances have been removed?
For example our application creates and destroys 'things' at runtime that I
want to add and remove from the perfmon instance list. The first instances
appear but I'm unable to get the new instances (same name & different names)
to appear! Even if I restart my process (Windows Service) I'm unable to even
add the first instances back. I'm forced to reboot the server!
Does anyone have any suggestions? Anything to do with object references?
|
|
|
|
|
Why does dotnet add the instances to performance counter all in lower case?
Other counters on the system seem to use mixed case.
Also simple changing the InstanceName and setting its value does not seem to
be enough to create a new instance under perfmon. It appears as though you
must create new PerformanceCounter objects for each instance. Not what the
documentation states though!
|
|
|
|
|
solidstore wrote:
Also simple changing the InstanceName and setting its value does not seem to
be enough to create a new instance under perfmon. It appears as though you
must create new PerformanceCounter objects for each instance. Not what the
documentation states though!
Wrong! If you look at the IL, you will see the last 2 instructions:
L_003e: call PerformanceCounter.Close
L_0043: ret
Now look at the NextValue call tree (thanks to Reflector). You will notice that PerfomanceCounter.Initialize() is called everytime, hence being closed, it will re-initialize.
MyDUMeter: a .NET DUMeter clone
|
|
|
|
|
I'm working with PerformanceCounter and I'm noticing very strange behaviour.
I can't add any instances to my counters? I used to have a single _Total
instance but now I change my code so that there is meant to be several
instances. I've followed all the examples etc... but no joy. I understand
the instance is meant to be created when you first set the counters value
and that you can reuse the PerformanceCounter object for multiple instances
by just changing the InstanceName before setting the RawValue. Does anyone
have any suggestions?
|
|
|
|
|
Is it possible to write .NET applets similar to java applets?
--
Ignorant people upsets me.
|
|
|
|
|
With the previous version of Visual Basic, it was possible write ActiveX Controls that are functionally equivalent (and quite more powerful too!) to Java Applets.
With the current .NET framework, I think it should be possible to develop a control and host it from within Internet Explorer. A search over CodeProject or Google should give some results. It should be possible, since if you query the useragent of Microsoft Internet Explorer after installation of .NET framework, it reports as:
Mozilla/4.0(compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705)
Deepak Kumar Vasudevan
http://deepak.portland.co.uk/
|
|
|
|
|
Deepak Kumar Vasudevan wrote:
since if you query the useragent of Microsoft Internet Explorer after installation of .NET framework
That's what triggered my question in the first place. I haven't found anything on the subject yet, but I must admit I didn't go out of my way to find anything either.
--
Ignorant people upsets me.
|
|
|
|
|
|
Thanks! I found an example somewhere on how to do it, but it was much more complicated than this and it had some major tradeoffs (can't remember what it was). This looks sweet and simple. I think I'll try it right away..
--
"It is amazing how f-ing crazy I really am."
|
|
|
|
|
Google for "SimpleControl".
Then google for "Chris Sells", because he's written a few articles on this subject (MSDN and MSDN magazine) Also look at http://www.sellsbrothers.com/wahoo[^],
Make sure you know something about .Net code access security, permissions and codegroups, because you're gonna see security permission exceptions...
VictorV
|
|
|
|
|
I am not too sure if this is the right place for this post but..
I am developing a desktop application.
It is using JET as it's backend database. The mdb file typically resides on the local machine. I wanted to implement funcationaility where the mdb file could be shared. I tried to put a UNC (\\computer\my.mdb) as my path but it will not work. I know that the permissions are right on the other computer and all of that suff. I can open it no problem. Is the .NET Framework Security stuff restricting the files that I can access over the intranet? I know it does it with assemblies.
Any ideas?
Thanks,
Jonathan
|
|
|
|