// Portions copyright 2005 - 2007: Diego Guidi
// Portions copyright 2006 - 2008: Rory Plaire (codekaizen@gmail.com)
//
// This file is part of GeoAPI.
// GeoAPI is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// GeoAPI is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public License
// along with GeoAPI; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#if NETCF
using System;
namespace GeoAPI.DataStructures
{
/// <summary>
/// A supoort class: the purpose is to integrate System.BitConverter
/// methods not presents in .NET Compact Framework.
/// </summary>
public class BitConverter
{
public static Int64 DoubleToInt64Bits(Double x)
{
#if UNSAFE
unsafe
{
return *(Int64*)&x;
}
#else
Byte[] bytes = System.BitConverter.GetBytes(x);
Int64 value = System.BitConverter.ToInt64(bytes, 0);
return value;
#endif
}
public static Double Int64BitsToDouble(Int64 x)
{
#if UNSAFE
unsafe
{
return *(Double*)&x;
}
#else
Byte[] bytes = System.BitConverter.GetBytes(x);
Double value = System.BitConverter.ToDouble(bytes, 0);
return value;
#endif
}
}
}
#endif
Month: February 2011
Working with the Conditional Attribute
using System;
using System.Diagnostics;
public class TestClass
{
public void Method1()
{
Console.WriteLine("Hello from Method1!");
}
[Conditional("DEBUG")]
public void Method2()
{
Console.WriteLine("Hello from Method2!");
}
public void Method3()
{
Console.WriteLine("Hello from Method3!");
}
}
class MainClass
{
public static void Main()
{
TestClass MyTestClass = new TestClass();
MyTestClass.Method1();
MyTestClass.Method2();
MyTestClass.Method3();
}
}
Defensive Programming:Asserts
// compile with: csc /r:system.dll file_1.cs
using System;
using System.Diagnostics;
class MyClass
{
public MyClass(int i)
{
this.i = i;
}
[Conditional("DEBUG")]
public void VerifyState()
{
Debug.Assert(i == 0, "Bad State");
}
int i = 0;
}
public class DefensiveProgrammingAsserts
{
public static void Main()
{
Debug.Listeners.Clear();
Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
MyClass c = new MyClass(1);
c.VerifyState();
}
}
Use Debug fail method
using System;
using System.Collections;
using System.Diagnostics;
class Class1 {
static void Main(string[] args) {
Debug.Fail("Fail Message", "An unexpected exception has been thrown.
");
}
}
Illustrates refusing permissions
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example19_4.cs illustrates refusing permissions
*/
using System;
using System.IO;
using System.Security.Permissions;
[assembly:FileIOPermissionAttribute(SecurityAction.RequestRefuse,
Unrestricted=true)]
public class Example19_4
{
public static void Main()
{
// Create a new file to work with
FileStream fsOut = File.Create(@"c: emp est.txt");
// Create a StreamWriter to handle writing
StreamWriter sw = new StreamWriter(fsOut);
// And write some data
sw.WriteLine("'Twas brillig, and the slithy toves");
sw.WriteLine("Did gyre and gimble in the wabe.");
sw.Flush();
sw.Close();
}
}
Illustrates requesting a permission set
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example19_3.cs illustrates requesting a permission set
*/
using System;
using System.IO;
using System.Security.Permissions;
[assembly:PermissionSetAttribute(SecurityAction.RequestMinimum,
Name="FullTrust")]
public class Example19_3
{
public static void Main()
{
// Create a new file to work with
FileStream fsOut = File.Create(@"c: emp est.txt");
// Create a StreamWriter to handle writing
StreamWriter sw = new StreamWriter(fsOut);
// And write some data
sw.WriteLine("'Twas brillig, and the slithy toves");
sw.WriteLine("Did gyre and gimble in the wabe.");
sw.Flush();
sw.Close();
}
}
Illustrates requesting optional permissions
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example19_2.cs illustrates requesting optional permissions
*/
using System;
using System.IO;
using System.Security.Permissions;
[assembly:FileIOPermissionAttribute(SecurityAction.RequestOptional,
All=@"c: emp")]
public class Example19_2
{
public static void Main()
{
// Create a new file to work with
FileStream fsOut = File.Create(@"c: emp est.txt");
// Create a StreamWriter to handle writing
StreamWriter sw = new StreamWriter(fsOut);
// And write some data
sw.WriteLine("'Twas brillig, and the slithy toves");
sw.WriteLine("Did gyre and gimble in the wabe.");
sw.Flush();
sw.Close();
}
}

