Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

download script to hide URL in asp.net using c#

// download script to hide URL in asp.net using c#

protected void DownloadFile(string name)
{
string _fileName;
string _path = Request.PhysicalApplicationPath + "Upload/" + name;
System.IO.FileInfo _file = new System.IO.FileInfo(_path);
if (_file.Exists)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + _file.Name);
Response.AddHeader("Content-Length", _file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(_file.FullName);
Response.End();
}
else
{
ClientScript.RegisterStartupScript(Type.GetType("System.String"), "messagebox", "&lt;script type=\"text/javascript\"&gt;alert('File not Found');</script>");
}

C# & Databases - Creating a unique identifer

Creating a new GUID in C# is not as straightforward as one would think. Calling the constructor of System.Guid() will produce a GUID of all zeroes. Using System.Guid.NewGuid() will produce the desired effect of a new, unique identifier.

// produces a "blank" GUID: '00000000-0000-0000-0000-000000000000'
System.Guid blankGuid = new System.Guid(); 

// produces a new, unique GUID: '96d36892-6eed-400c-91f8-a194f3522fae'
System.Guid desiredGuid = System.Guid.NewGuid(); 

C# Reflection - Dealing with AmbiguousMatchException

C# code invoking a static method with reflection and dealing with an AmbiguousMatchException
Source: http://msdn2.microsoft.com/en-US/library/xthb9284.aspx


class Myambiguous {
    //The first overload is typed to an Int32
    public static void Mymethod (Int32 number){
       Console.Write("\n{0}", "I am from Int32 method");
    }

    //The second overload is typed to a string
    public static void Mymethod (string alpha) {
       Console.Write("\n{0}", "I am from a string.");
    }
    
    public static void Main() {
        try {
            //The following does not cause as exception
            Mymethod (2);  // goes to Mymethod (Int32)
            Mymethod ("3");   // goes to Mymethod (string)

            Type Mytype = Type.GetType("Myambiguous");

            MethodInfo Mymethodinfo32 = Mytype.GetMethod("Mymethod", new Type[]{typeof(Int32)});
            MethodInfo Mymethodinfostr = Mytype.GetMethod("Mymethod", new Type[]{typeof(System.String)});

            //Invoke a method, utilizing a Int32 integer
            Mymethodinfo32.Invoke(null, new Object[]{2});

            //Invoke the method utilizing a string
            Mymethodinfostr.Invoke(null, new Object[]{"1"});

            //The following line causes an ambiguious exception
            MethodInfo Mymethodinfo = Mytype.GetMethod("Mymethod");
        }  // end of try block
  
        catch(System.Reflection.AmbiguousMatchException theException) {
            Console.Write("\nAmbiguousMatchException message - {0}", theException.Message);
        }
        catch {
            Console.Write("\nError thrown");
        }
        return;
    }
}
 
 //This code produces the following output:
 //I am from Int32 method
 //I am from a string.
 //I am from Int32 method
 //I am from a string.
 //AmbiguousMatchException message - Ambiguous match found.

Skeleton c# class with test harness (

Using SnippetCompiler (http://www.sliver.com) a lot, I needed a template which has the nUnit components started... Here it is.

using System;
using System.Collections;
using NUnit.Framework;

#region entry point
public class EntryClass
{
	public static void Main()
	{
		NewClass t = new NewClass();
	}

}
#endregion entry point

// To test, select Build|Build To File... then point nunit's guii at the resultant dll


// Class we will build up for testing
public class NewClass
{
    // Sample Method which is tested
    public int MyMethod(int firstNumber, int secondNumber)
    {
        return firstNumber + secondNumber;
    }
}


// Class that does the nunit testing ([TestFixture] tells nunit it's the test class)
[TestFixture]
public class MyNewClassTestClass
{    
        // Method for testing the method in the class above ([Test] tells nunit this is a test method)
		[Test]
		public void MyMethodTest() 
		{
            // create an instance of the class
			NewClass obj_newClass = new NewClass();
            
            // Verify assertion
			Assertion.Assert(obj_newClass.MyMethod(10, 20) == 30);
		}    

} 

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS