– 델리게이트를 통해 특정 작업을 캡슐화할 수 있다.

델리게이트 인스턴스에 어떤 메서드를 사용할 수 있는지는 델리게이트 타입의 선언에 따라 다르다.
– 델리게이트 인스턴스 생성을 위해서는 호출할 메서드와 (인스턴스 메서드의 경우) 메서드를 호출할 대상이 필요하다.
– 델리게이트 인스턴스는 불변성(immutable)을 갖는다. 한번 생성되고 나면 변경될 수 없다. 이렇기 때문에 델리게이트를 다른 델리게이트와 합친다거나 다른 메서드의 파라미터로 넘길 때 쓰레드 안정성이나 일관성 유지에 대해 고민하지 않아도 된다.(기존의 인스턴스를 합쳐서 새 인스턴스를 생성하면서 원본 인스턴스는 변경하지 않는다.)
– 각각의 델리게이트 인스턴스는 내부적으로 호출 리스트, 즉 동작의 리스트를 갖는다.
– 델리게이트 인스턴스들을 함께 조합하거나 다른 인스턴스에서 제거할 수 있다.
– 델리게이트 인스턴스에
Read More
C# 2.0의 CLR에 통합된 Nullable Value Type을 지원한다.
이미 알다 시피 Strongly-Typed Language인 C#에서 Value Type에 Null을 대입할 수는 없다.
Int32 a = null;
대입을 하게 되면 컴파일시에 “오류 CS0037: null을 허용하지 않는 값 형식이므로 null을 변환할 수 없습니다.”를 발생하게 된다.
Reference Type에는 당연히 Null이 허용된다.
object o = null;
그럼 Value Type에는 null을 대입하지 않으면 되는거 아닌가?
현실의 문제점은 데이터베이스가 nullable을 허용하고, 데이터베이스로 부터 가져온 null을 CLR이 표현할 방법이 없다는데 있다.
또한 Java에서는 DateTime 이 Reference type 이지만, CLR은 ValueType으로써 웹서비스 등으로 자바로 부터 가져온 DateTime의 Null에 대해 대응 할 방법이 없다.
이 상황을 발전 시켜
Read More
폰트파일 로드
PrivateFontCollection fonts;
FontFamily family = LoadFontFamily(@"F:\azuki.ttf", out fonts);
theFont = new Font(family, 20.0f);
// when done:
theFont.Dispose();
family.Dispose();
family.Dispose();

/////////////////////////////////////////////////////////////////////////////////////////

public static FontFamily LoadFontFamily(Stream stream, out PrivateFontCollection fontCollection)
{
var buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
return LoadFontFamily(buffer, out fontCollection);
}

//public static unsafe FontFamily LoadFontFamilyUnsafe(byte[] buffer, out PrivateFontCollection fontCollection)
//{
// fixed (byte* ptr = buffer)
// {
// fontCollection = new PrivateFontCollection();
// fontCollection.AddMemoryFont(new IntPtr(ptr), buffer.Length);
// return fontCollection.Families[0];
// }
//}

public static FontFamily LoadFontFamily(byte[] buffer, out PrivateFontCollection fontCollection)
{
// pin array so we can get its address
var handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
Read More
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Configuration.Install;

namespace WindowsApplication
{
[System.ComponentModel.RunInstaller(true)]
public class SetupInstaller : System.Configuration.Install.Installer
{
public SetupInstaller()
{
this.AfterInstall += new InstallEventHandler(SetupInstaller_AfterInstall);
this.BeforeInstall += new InstallEventHandler(SetupInstaller_BeforeInstall);
this.Committed += new InstallEventHandler(SetupInstaller_Committed);
}

// 셋업 설치가 완료되었을 때 호출
void SetupInstaller_Committed(object sender, InstallEventArgs e)
{
// 아래 그림 3의 CustomActionData 내의 값을 읽어온다.
string name = Context.Parameters["CUSTOM1"];
string age = Context.Parameters["CUSTOM2"];

MessageBox.Show(name + "\r\n" + age);
}

// WindowsApplication.exe 파일이 설치되기 직전에 호출
void SetupInstaller_BeforeInstall(object sender, InstallEventArgs e)
{
MessageBox.Show("beforeInstall");
}

// WindowsApplication.exe 파일이 설치된후에 호출
void SetupInstaller_AfterInstall(object sender, InstallEventArgs e)
{
MessageBox.Show("afterInstall");
}
}
}

[그림 …

Read More

출처 : http://www.codeproject.com/KB/tips/cs_imagedownload.aspx

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Text;

public class DownloadImage {
private string imageUrl;
private Bitmap bitmap;
public DownloadImage(string imageUrl) {
this.imageUrl = imageUrl;
}
public void Download() {
try {
WebClient client = new WebClient();
Stream stream = client.OpenRead(imageUrl);
bitmap = new Bitmap(stream);
}
catch (Exception e) {
Console.WriteLine(e.Message);
}
}
public Bitmap GetImage() {
return bitmap;
}
public void SaveImage(string filename, ImageFormat format) {
if (bitmap != null) {
bitmap.Save(filename, format);
}
}
}
Read More
public enum DaysOfWeek
{
Sunday = 1,
Monday = 2,
Tuesday = 3,
Wednesday = 4,
Thursday = 5,
Friday = 6,
Saturday = 7
}
[/CODE]

[CODE]string CurrentDayString = "Sunday";
DaysOfWeek CurrentDay = DaysOfWeek.Monday;
try
{
CurrentDay = (DaysOfWeek)Enum.Parse(typeof(DaysOfWeek), CurrentDayString);} catch
{
// Invalid enumeration value
}

switch (CurrentDay)
{
case DaysOfWeek.Saturday:
case DaysOfWeek.Sunday:
// What are you doing working on the weekend
break;

default:
// Get back to work
break;
}
Read More

DefaultTraceListener
이 클래스의 객체는 Trace와 Debug 클래스의 Listeners 컬렉션에 자동으로 추가되며, Visual Studio.NET의 출력 창 또는 메시지 상자에 메시지를 출력하는 기능을 수행한다.

TextWriterTraceListener
이 클래스의 객체는 Stream 클래스로부터 파생된 클래스에 메시지를 출력한다. 따라서 콘솔이나 파일로 메시지를 출력할 때 사용할 수 있다.

EventLogTraceListener
이 클래스의 객체에서는 윈도우즈 운영체제의 이벤트 로그로 메시지를 출력한다.
[CODE]Debug.Listeners.Add(new EventLogTraceListener(“source”));
Debug.Listeners.Add(new TextWriterTraceListener(@”d:\log.txt”));
Debug.Listeners.Add(new TextWriterTraceListener(System.Console.Out));
Debug.Assert(false, “fail message”, “fail detailmessage”);
[/CODE]

Read More