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(); } }
Development Class
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(); } }
Illustrates requesting minimum permissions
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example19_1.cs illustrates requesting minimum permissions */ using System; using System.IO; using System.Security.Permissions; [assembly:FileIOPermissionAttribute(SecurityAction.RequestMinimum, All=@"c: emp")] public class Example19_1 { 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(); } }