<?xml version="1.0" encoding="UTF-8" ?> <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C#</title><link>http://blogs.wankuma.com/hirase/category/1700.aspx</link><description>C#</description><managingEditor>T.Hirase</managingEditor><dc:language>ja-JP</dc:language><generator>.Text Version 0.95.2004.102</generator><item><dc:creator>T.Hirase</dc:creator><title>[C++/C#] インタフェースっていったい・・・？</title><link>http://blogs.wankuma.com/hirase/archive/2008/07/14/148849.aspx</link><pubDate>Mon, 14 Jul 2008 22:00:00 GMT</pubDate><guid>http://blogs.wankuma.com/hirase/archive/2008/07/14/148849.aspx</guid><wfw:comment>http://blogs.wankuma.com/hirase/comments/148849.aspx</wfw:comment><comments>http://blogs.wankuma.com/hirase/archive/2008/07/14/148849.aspx#Feedback</comments><slash:comments>8</slash:comments><wfw:commentRss>http://blogs.wankuma.com/hirase/comments/commentRss/148849.aspx</wfw:commentRss><trackback:ping>http://blogs.wankuma.com/hirase/services/trackbacks/148849.aspx</trackback:ping><description>&lt;h4&gt;C++0xのコンセプトの話&lt;/h4&gt;
&lt;p&gt;&lt;a href="http://d.hatena.ne.jp/faith_and_brave/"&gt;アキラさん&lt;/a&gt;のC++0xの資料を拝見して、コンセプトって、なんでインタフェースっていう名前にならなかったのか疑問に思ったのは自分だけ？&lt;/p&gt;
&lt;p&gt;だって、インタフェースと機能は同じで、しばりが少ないってだけな印象なのに。。同じダックタイピングなのに。&lt;/p&gt;
&lt;p&gt;いうなれば、コンセプトがインタフェースらしく振舞うのであれば、それは最早インタフェースだと思う。&lt;/p&gt;
&lt;h4&gt;C#のインタフェースの話&lt;/h4&gt;
&lt;p&gt;なんで継承/実装していないとキャストできないのか不思議。明示的に継承していない場合でも、関係なしにダウンキャストできていいと思うのですが。。&lt;/p&gt;
&lt;p&gt;こんな感じ↓&lt;/p&gt;
&lt;pre class="csharp" xml:space="preserve" name="sourceCode"&gt;
using System;
interface IMyMeMine
{
    void Hello();
}

class SayHello
{
    public void Hello() { Console.WriteLine("Hello"); }
}

class Program
{
    static void Main(String[] args)
    {
        Hello(new SayHello());
    }

    static void Hello&amp;lt;T&amp;gt;(T t) where T:IMyMeMine
    {
        t.Hello();
    }
}
&lt;/pre&gt;
&lt;p&gt;たとえばシステムのクラスをジェネリックに扱いたい場合に、ときどきやりたい。&lt;/p&gt;&lt;img src ="http://blogs.wankuma.com/hirase/aggbug/148849.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>T.Hirase</dc:creator><title>[C#] Google Protocol Buffers Serializer</title><link>http://blogs.wankuma.com/hirase/archive/2008/07/09/148170.aspx</link><pubDate>Wed, 09 Jul 2008 16:10:00 GMT</pubDate><guid>http://blogs.wankuma.com/hirase/archive/2008/07/09/148170.aspx</guid><wfw:comment>http://blogs.wankuma.com/hirase/comments/148170.aspx</wfw:comment><comments>http://blogs.wankuma.com/hirase/archive/2008/07/09/148170.aspx#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://blogs.wankuma.com/hirase/comments/commentRss/148170.aspx</wfw:commentRss><trackback:ping>http://blogs.wankuma.com/hirase/services/trackbacks/148170.aspx</trackback:ping><description>&lt;p&gt;&lt;a href="http://code.google.com/apis/protocolbuffers/"&gt;Google Protocol Buffers&lt;/a&gt;が公開されたとのことで、C#版のシリアライザ（仕様の確認はまた今度）を作ってみました。&lt;/p&gt;
&lt;pre class="csharp" xml:space="preserve" name="sourceCode"&gt;
namespace ProtocolBuffers
{
    using System;
    using System.IO;
    using System.Text;

    public class Serializer
    {
        #region Serialize
        public static void Serialize&amp;lt;TSerialize&amp;gt;(StreamWriter writer, TSerialize instance)
        {
            SerializeInternal(writer, instance.GetType(), instance, 0);
        }

        public static void Serialize(StreamWriter writer, Type type, Object instance)
        {
            SerializeInternal(writer, type, instance, 0);
        }

        private static void SerializeInternal(StreamWriter writer, Type type, Object instance, Int32 depth)
        {
            if (writer == null) throw new System.ArgumentNullException(&amp;quot;writer&amp;quot;);
            if (type == null) throw new System.ArgumentNullException(&amp;quot;type&amp;quot;);
            if (instance == null) throw new System.ArgumentNullException(&amp;quot;instance&amp;quot;);

            WriteIndent(writer, depth);
            writer.Write(type.Name);
            writer.WriteLine(&amp;quot;{&amp;quot;);
            foreach (var propertyInfo in type.GetProperties())
            {
                if (!propertyInfo.CanRead) continue;

                var propertyType = propertyInfo.PropertyType;
                var propertyName = propertyInfo.Name;
                var propertyValue = propertyInfo.GetValue(instance, null) ?? String.Empty;
                if (IsSystemType(propertyType))
                {
                    WriteIndent(writer, depth + 1);
                    writer.WriteLine(&amp;quot;{0} : \&amp;quot;{1}\&amp;quot;&amp;quot;, propertyName, propertyValue.ToString().Replace(&amp;quot;\&amp;quot;&amp;quot;, &amp;quot;\\\&amp;quot;&amp;quot;));
                }
                else
                {
                    SerializeInternal(writer, propertyType, propertyValue, depth + 1);
                }
            }
            WriteIndent(writer, depth);
            writer.WriteLine(&amp;quot;}&amp;quot;);
        }
        private static void WriteIndent(StreamWriter writer, Int32 depth)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(' ', depth);
            writer.Write(sb.ToString());
        }
        #endregion
        #region Utility
        private static Boolean IsSystemType(Type type)
        {
            return type.Namespace.StartsWith(&amp;quot;System&amp;quot;);
        }
        #endregion
    }
}
&lt;/pre&gt;
&lt;p&gt;使い方は、以下&lt;/p&gt;
&lt;pre class="csharp" xml:space="preserve" name="sourceCode"&gt;
namespace ProtocolBuffers
{
    using System;
    using System.IO;

    public class Test
    {
        public String Name { get; set; }
        public String EMail { get; set; }
    }

    public class Test2
    {
        public Test Test { get; set; }
        public Int32 Hash { get { return this.GetHashCode(); } }
        public DateTime DateTime { get { return DateTime.Now; } }
    }

    class TestProgram
    {
        static void Main(string[] args)
        {
            var testObject = new Test { Name = "Hirase", EMail = "hirase@example.com" };
            var test2Object = new Test2 { Test = testObject };
            using (var writer = new StreamWriter(@"test.pb"))
            {
                Serializer.Serialize&amp;lt;Test2&amp;gt;(writer, test2Object);
            }
        }
    }
}
&lt;/pre&gt;
&lt;p&gt;結果は以下&lt;/p&gt;
&lt;pre&gt;
Test2{
 Test{
  Name : &amp;quot;Hirase&amp;quot;
  EMail : &amp;quot;hirase@example.com&amp;quot;
 }
 Hash : &amp;quot;54267293&amp;quot;
 DateTime : &amp;quot;2008/07/09 14:37:08&amp;quot;
}
&lt;/pre&gt;
&lt;img src ="http://blogs.wankuma.com/hirase/aggbug/148170.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>T.Hirase</dc:creator><title>[C#] SplitContainer/SplitterPanel 使いにくい！</title><link>http://blogs.wankuma.com/hirase/archive/2008/07/02/146647.aspx</link><pubDate>Wed, 02 Jul 2008 01:20:00 GMT</pubDate><guid>http://blogs.wankuma.com/hirase/archive/2008/07/02/146647.aspx</guid><wfw:comment>http://blogs.wankuma.com/hirase/comments/146647.aspx</wfw:comment><comments>http://blogs.wankuma.com/hirase/archive/2008/07/02/146647.aspx#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://blogs.wankuma.com/hirase/comments/commentRss/146647.aspx</wfw:commentRss><trackback:ping>http://blogs.wankuma.com/hirase/services/trackbacks/146647.aspx</trackback:ping><description>&lt;p&gt;つぶやき、ですが・・・&lt;/p&gt;
&lt;p&gt;.NET Framework 2.0 から追加されました SplitContainer/SplitterPanel が異常に使いにくい。&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;SplitContainer.Panel1Collapsed / Panel2Collapsed プロパティでパネルの開閉するって、どうかしてるよ。&lt;/li&gt;
&lt;li&gt;SplitterPanel が sealed クラスなのは、どうかしてるよ。&lt;/li&gt;
&lt;li&gt;SplitterContainer.Panel1 / Panel2 からしか各パネルにアクセスできないって、どうかしてるよ。&lt;/li&gt;
&lt;li&gt;SplitContainer の両方のパネルを閉じられないって、どうかしてるよ。&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;順を追ってみていきます。&lt;/p&gt;
&lt;h4&gt;SplitContainer.Panel1Collapsed / Panel2Collapsed プロパティでパネルの開閉するって、どうかしてるよ。&lt;/h4&gt;
&lt;p&gt;「メソッドでヤラせろよ。」とか、「SplitterPanel.Collapsed プロパティ（internal）を使わせろ。」とか、そういう話。というか、どうして、「Panels」がないんだ！　信じられない！&lt;/p&gt;
&lt;h4&gt;SplitterPanel が sealed クラスなのは、どうかしてるよ。&lt;/h4&gt;
&lt;p&gt;あぁ、もう最悪。継承できない。（SplitContainer を継承して、無駄なものを作ることは可能）&lt;/p&gt;
&lt;h4&gt;SplitterContainer.Panel1 / Panel2 からしか各パネルにアクセスできないって、どうかしてるよ。&lt;/h4&gt;
&lt;p&gt;我求ム「Panels」。ま、内部的には「Controls」に「Panel1」と「Panel2」が入っているのは知っているけど、いちいちキャストせにゃならんじゃん。いうか、パネル２つしかないのってもったいないよ！&lt;/p&gt;
&lt;h4&gt;SplitContainer の両方のパネルを閉じられないって、どうかしてるよ。&lt;/h4&gt;
&lt;p&gt;これ、ちょっと特殊だけど、両方のパネルを閉じたときには、その SplitContainer の親要素が SplitterPanel であれば、その SplitterPanel を閉じて欲しいのよね（そうじゃないときは、例外発生か、閉じられないか）。&lt;/p&gt;
&lt;p&gt;以上&lt;/p&gt;
&lt;img src ="http://blogs.wankuma.com/hirase/aggbug/146647.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>T.Hirase</dc:creator><title>[C#] かっこをつける。</title><link>http://blogs.wankuma.com/hirase/archive/2008/04/17/133579.aspx</link><pubDate>Thu, 17 Apr 2008 11:01:00 GMT</pubDate><guid>http://blogs.wankuma.com/hirase/archive/2008/04/17/133579.aspx</guid><wfw:comment>http://blogs.wankuma.com/hirase/comments/133579.aspx</wfw:comment><comments>http://blogs.wankuma.com/hirase/archive/2008/04/17/133579.aspx#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://blogs.wankuma.com/hirase/comments/commentRss/133579.aspx</wfw:commentRss><trackback:ping>http://blogs.wankuma.com/hirase/services/trackbacks/133579.aspx</trackback:ping><description>&lt;p&gt;格好をつけるためでなく、実際的な問題として、括弧をつけることを忘れて酷いことに。&lt;/p&gt;
&lt;pre class="csharp" xml:space="preserve" name="sourceCode"&gt;Int32 x = 5, y = 4;

if (x == 1)
    if (y == 2) Console.WriteLine("x==1 &amp;&amp; y==2");
else if (x == 2)
    if (y == 3) Console.WriteLine("x==2 &amp;&amp; y==3");
else
    if (y == 4) Console.WriteLine("y == 4");

Console.WriteLine("End");
&lt;/pre&gt;
&lt;p&gt;出力は、「End」のみ。&lt;/p&gt;&lt;img src ="http://blogs.wankuma.com/hirase/aggbug/133579.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>T.Hirase</dc:creator><title>[C#] String文字列からLinq実行</title><link>http://blogs.wankuma.com/hirase/archive/2008/04/08/132255.aspx</link><pubDate>Tue, 08 Apr 2008 18:17:00 GMT</pubDate><guid>http://blogs.wankuma.com/hirase/archive/2008/04/08/132255.aspx</guid><wfw:comment>http://blogs.wankuma.com/hirase/comments/132255.aspx</wfw:comment><comments>http://blogs.wankuma.com/hirase/archive/2008/04/08/132255.aspx#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://blogs.wankuma.com/hirase/comments/commentRss/132255.aspx</wfw:commentRss><trackback:ping>http://blogs.wankuma.com/hirase/services/trackbacks/132255.aspx</trackback:ping><description>&lt;p&gt;Regexのテスターみたいに、Linqのテスターを作ろうと思ったら、StringからLinqする術がわからなかった。&lt;/p&gt;
&lt;p&gt;ので、ゴリゴリ手書き。もっと良い方法があればフィードバックプリーズ。&lt;/p&gt;
&lt;pre class="csharp" xml:space="preserve" name="sourceCode"&gt;using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using Microsoft.CSharp;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("====== String文字列からLinq実行。 ======");

        // パート1
        var data1 = new Int32[] { 10, 11, 14, 18, 19, 21, 22, 28 };
        Console.Write("データ1: ");
        foreach (var value in data1) Console.Write(value + ",");
        Console.WriteLine();
        var res1 = LinqFrom&amp;lt;Int32&amp;gt;("from v in data where predicater(v) select v;"
            , "TempNamespace", "TempClass", "TempFunc"
            , data1, delegate(Int32 v) { return v % 2 == 0; });
        Console.Write("データ1の偶数要素： ");
        foreach (var value in res1) Console.Write(value + ",");
        Console.WriteLine();
        Console.WriteLine();

        // パート2
        var data2 = new String[] { "RedHat", "Fedora", "Mac", "Windows XP", "Windows Vista" };
        Console.Write("データ2: ");
        foreach (var value in data2) Console.Write(value + ",");
        Console.WriteLine();
        var res2 = LinqFrom&amp;lt;String&amp;gt;("from v in data where predicater(v) select v;"
            , "TempNamespace", "TempClass", "TempFunc"
            , data2, delegate(String v) { return v.StartsWith("Windows"); });
        Console.Write("データ2のWindows： ");
        foreach (var value in res2) Console.Write(value + ",");
        Console.WriteLine();
    }

    public static IEnumerable&amp;lt;T&amp;gt; LinqFrom&amp;lt;T&amp;gt;(String linqText, String tempNamespace, String tempClassName, String tempFuncName, IEnumerable&amp;lt;T&amp;gt; data, Predicate&amp;lt;T&amp;gt; predicater)
    {
        CodeDomProvider provider = new CSharpCodeProvider(new Dictionary&amp;lt;string, string&amp;gt;() { { "CompilerVersion", "v3.5" } });
        CompilerParameters cp = new CompilerParameters();
        cp.GenerateInMemory = true;
        cp.ReferencedAssemblies.Add("System.Core.dll");
        cp.ReferencedAssemblies.Add("System.Xml.Linq.dll");

        String compileSource =
          "using System.Linq;" + System.Environment.NewLine
        + "namespace " + tempNamespace + " {" + System.Environment.NewLine
        + "    public class " + tempClassName + System.Environment.NewLine
        + "    {" + System.Environment.NewLine
        + "        public System.Collections.Generic.IEnumerable&amp;lt;" + typeof(T).FullName + "&amp;gt; " + tempFuncName + "(System.Collections.Generic.IEnumerable&amp;lt;" + typeof(T).FullName + "&amp;gt; data, System.Predicate&amp;lt;" + typeof(T).FullName + "&amp;gt; predicater)" + System.Environment.NewLine
        + "        {" + System.Environment.NewLine
        + "            return " + linqText + System.Environment.NewLine
        + "        }" + System.Environment.NewLine
        + "    }" + System.Environment.NewLine
        + "}" + System.Environment.NewLine;
        CompilerResults cr = provider.CompileAssemblyFromSource(cp, compileSource);
        if (cr.Errors.HasErrors)
        {
            throw new System.Exception("構文エラーらしいよ。");
        }
        else
        {
            Assembly asm = cr.CompiledAssembly;
            Type tempClassType = asm.GetType(tempNamespace + "." + tempClassName);
            Object classInstance = Activator.CreateInstance(tempClassType);
            MethodInfo methodInfo = tempClassType.GetMethod(tempFuncName);
            Object result = methodInfo.Invoke(classInstance, BindingFlags.InvokeMethod, null, new Object[] { data, predicater }, CultureInfo.CurrentCulture);
            return (System.Collections.Generic.IEnumerable&amp;lt;T&amp;gt;)result;
        }
    }
}
&lt;/pre&gt;&lt;img src ="http://blogs.wankuma.com/hirase/aggbug/132255.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>T.Hirase</dc:creator><title>[C#] 名前付き定数・・・？</title><link>http://blogs.wankuma.com/hirase/archive/2008/03/28/130154.aspx</link><pubDate>Fri, 28 Mar 2008 01:01:00 GMT</pubDate><guid>http://blogs.wankuma.com/hirase/archive/2008/03/28/130154.aspx</guid><wfw:comment>http://blogs.wankuma.com/hirase/comments/130154.aspx</wfw:comment><comments>http://blogs.wankuma.com/hirase/archive/2008/03/28/130154.aspx#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://blogs.wankuma.com/hirase/comments/commentRss/130154.aspx</wfw:commentRss><trackback:ping>http://blogs.wankuma.com/hirase/services/trackbacks/130154.aspx</trackback:ping><description>&lt;p&gt;名前つきの何かを作るためのAttribute。&lt;/p&gt;
&lt;p&gt;たとえば、下の例のように、表示名だけを変えたいようなケースとか、もう少し拡張すれば、多言語化アプリ作成にも役立ちそう。&lt;/p&gt;
&lt;pre class="csharp" xml:space="preserve" name="sourceCode"&gt;using System;
using System.Reflection;

namespace Sample
{
    internal class Program
    {
        enum Windows
        {
            [Name("Windows 95")]
            Chicago,
            [Name("Windows 95 OSR2")]
            Detroit,
            [Name("Windows 98")]
            Memphis,
            [Name("Windows 2000")]
            Cairo,
            [Name("Windows Me")]
            Georgia,
            [Name("Windows XP")]
            Whistler,
            [Name("Windows Vista")]
            Longhorn,
        }

        private static void Main(string[] args)
        {
            foreach (object value in Enum.GetValues(typeof(Windows)))
            {
                String name = NameAttribute.GetName(value as Enum);
                Console.WriteLine(name);
            }
        }
    }

    [AttributeUsage(AttributeTargets.All)]
    public class NameAttribute : Attribute
    {
        public String Name
        {
            get;
            private set;
        }

        public NameAttribute(String name)
        {
            this.Name = name;
        }

        public static String GetName(MemberInfo type)
        {
            Attribute[] attributes;
            attributes = type.GetCustomAttributes(typeof(NameAttribute), true) as Attribute[];
            if (attributes == null || attributes.Length == 0)
                return null;

            NameAttribute nameAttribute = attributes[0] as NameAttribute;
            return nameAttribute.Name;
        }

        public static String GetName(Enum enumValue)
        {
            Type enumType = enumValue.GetType();
            String enumName = Enum.GetName(enumType, enumValue);

            return GetName(enumType.GetField(enumName));
        }
    }
}&lt;/pre&gt;&lt;img src ="http://blogs.wankuma.com/hirase/aggbug/130154.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>T.Hirase</dc:creator><title>[C#] 自分でオブジェクトをシリアライズ/デシリアライズする。</title><link>http://blogs.wankuma.com/hirase/archive/2008/03/27/129927.aspx</link><pubDate>Thu, 27 Mar 2008 02:01:00 GMT</pubDate><guid>http://blogs.wankuma.com/hirase/archive/2008/03/27/129927.aspx</guid><wfw:comment>http://blogs.wankuma.com/hirase/comments/129927.aspx</wfw:comment><comments>http://blogs.wankuma.com/hirase/archive/2008/03/27/129927.aspx#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://blogs.wankuma.com/hirase/comments/commentRss/129927.aspx</wfw:commentRss><trackback:ping>http://blogs.wankuma.com/hirase/services/trackbacks/129927.aspx</trackback:ping><description>&lt;p&gt;昨日に続き、実験。XmlSerializerを自前で実装してみます（簡易ですけど）。&lt;/p&gt;
&lt;pre class="csharp" xml:space="preserve" name="sourceCode"&gt;using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Text;
using System.Xml;

namespace Sample
{
    internal class Program
    {

        private static void Main(string[] args)
        {
            MyClass mc = new MyClass();
            mc.Value = "ぴょんぴょん";
            Serialize(mc, @"D:\hoge.xml");
            IEnumerable&amp;lt;Object&amp;gt; deserializedObjects = DeserializeObjects(@"D:\hoge.xml");
            foreach (object deserializedObject in deserializedObjects)
            {
                if (deserializedObject is MyClass)
                {
                    MyClass mc2 = deserializedObject as MyClass;
                    Debug.Assert(String.Equals(mc.Value, mc2.Value));
                }
            }
        }

        /// &amp;lt;summary&amp;gt;
        /// オブジェクトをファイルにシリアライズします。
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;param name="o"&amp;gt;シリアライズするオブジェクト&amp;lt;/param&amp;gt;
        /// &amp;lt;param name="filePath"&amp;gt;ファイル名&amp;lt;/param&amp;gt;
        public static void Serialize(Object o, String filePath)
        {
            using (StreamWriter streamWriter = new StreamWriter(filePath, false, Encoding.UTF8))
            {
                using (XmlWriter xmlWriter = XmlWriter.Create(streamWriter))
                {
                    Type type = o.GetType();
                    xmlWriter.WriteStartElement("class");
                    xmlWriter.WriteAttributeString("type", type.FullName);

                    ForEachPropertyInfo(type, delegate(PropertyInfo propertyInfo)
                        {
                            if (!propertyInfo.CanRead || !propertyInfo.CanWrite)
                                return;

                            Object propertyValue;
                            propertyValue = type.InvokeMember(propertyInfo.Name,
                                                              BindingFlags.GetProperty, null, o, null,
                                                              CultureInfo.CurrentCulture);
                            xmlWriter.WriteAttributeString(propertyInfo.Name, propertyValue.ToString());
                        });
                    xmlWriter.WriteEndElement();
                }
            }
        }

        /// &amp;lt;summary&amp;gt;
        /// ファイルに所定のフォーマットでシリアライズされたデータを
        /// デシリアライズします。
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;param name="filePath"&amp;gt;ファイル名&amp;lt;/param&amp;gt;
        public static IEnumerable&amp;lt;Object&amp;gt; DeserializeObjects(String filePath)
        {
            using (StreamReader streamReader = new StreamReader(filePath, Encoding.UTF8, true))
            {
                using (XmlReader xmlReader = XmlReader.Create(streamReader))
                {
                    while (xmlReader.Read())
                    {
                        if (!xmlReader.Name.Equals("class"))
                            continue;

                        String typeName = xmlReader.GetAttribute("type");
                        Type type = Type.GetType(typeName);
                        Object instance = Activator.CreateInstance(type);

                        ForEachPropertyInfo(type, delegate(PropertyInfo propertyInfo)
                            {
                                if (!propertyInfo.CanRead || !propertyInfo.CanWrite)
                                    return;
                                String value = xmlReader.GetAttribute(propertyInfo.Name);
                                type.InvokeMember(propertyInfo.Name,
                                                  BindingFlags.SetProperty, null, instance, new Object[] {value},
                                                  CultureInfo.CurrentCulture);
                            });

                        yield return instance;
                    }
                }
            }
        }

        /// &amp;lt;summary&amp;gt;
        /// 指定した型に含まれるプロパティを全て列挙し、Actionerに渡す。
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;param name="type"&amp;gt;プロパティを列挙したい型&amp;lt;/param&amp;gt;
        /// &amp;lt;param name="Actioner"&amp;gt;各プロパティを処理するPropertyInfoActioner&amp;lt;/param&amp;gt;
        private static void ForEachPropertyInfo(Type type, PropertyInfoActioner Actioner)
        {
            foreach (PropertyInfo propertyInfo in type.GetProperties())
            {
                Actioner(propertyInfo);
            }
        }
        private delegate void PropertyInfoActioner(PropertyInfo propertyInfo);

        #region Nested type: MyClass
        /// &amp;lt;summary&amp;gt;
        /// （テスト用）シリアライズするクラス
        /// &amp;lt;/summary&amp;gt;
        private class MyClass
        {
            private String value;

            public String Value
            {
                get { return value; }
                set { this.value = value; }
            }
        }
        #endregion
    }
}&lt;/pre&gt;&lt;img src ="http://blogs.wankuma.com/hirase/aggbug/129927.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>T.Hirase</dc:creator><title>[C#] 型名(String)から型(Type)を取得して、インスタンスを作る。</title><link>http://blogs.wankuma.com/hirase/archive/2008/03/26/129691.aspx</link><pubDate>Wed, 26 Mar 2008 01:33:00 GMT</pubDate><guid>http://blogs.wankuma.com/hirase/archive/2008/03/26/129691.aspx</guid><wfw:comment>http://blogs.wankuma.com/hirase/comments/129691.aspx</wfw:comment><comments>http://blogs.wankuma.com/hirase/archive/2008/03/26/129691.aspx#Feedback</comments><slash:comments>9</slash:comments><wfw:commentRss>http://blogs.wankuma.com/hirase/comments/commentRss/129691.aspx</wfw:commentRss><trackback:ping>http://blogs.wankuma.com/hirase/services/trackbacks/129691.aspx</trackback:ping><description>&lt;p&gt;C# で型名を System.String で与えられたときに、その型名から System.Type を取得します。&lt;/p&gt;
&lt;p&gt;追記：&lt;a href="http://msdn2.microsoft.com/library/w3f99sx1(VS.80).aspx"&gt;Type.GetType(String)&lt;/a&gt;を使っても同様の結果が得られます。このエントリは、実験的に、自分でも実装できるかをやってみたものです。&lt;/p&gt;
&lt;pre class="csharp" xml:space="preserve" name="sourceCode"&gt;
using System;
using System.Collections.Generic;

namespace Sample
{
    class Program
    {
        static void Main(string[] args)
        {
            // 型名から型を取得。
            Type type = GetTypeFromName("System.DateTime");

            // Typeからインスタンス作成。
            Object instance = Activator.CreateInstance(type); 
        }

        /// &amp;lt;summary&amp;gt;
        /// 特定の名前を持つ型を取得します。
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;param name="name"&amp;gt;型名&amp;lt;/param&amp;gt;
        /// &amp;lt;returns&amp;gt;取得した型&amp;lt;/returns&amp;gt;
        public static Type GetTypeFromName(String name)
        {
            return GetTypeFromName(name, GetCurrentDomainAssembliesType());
        }

        /// &amp;lt;summary&amp;gt;
        /// 特定の名前を持つ型を取得します。
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;param name="name"&amp;gt;型名&amp;lt;/param&amp;gt;
        /// &amp;lt;param name="types"&amp;gt;型のリスト&amp;lt;/param&amp;gt;
        /// &amp;lt;returns&gt;取得した型&amp;lt;/returns&amp;gt;
        public static Type GetTypeFromName(String name, IEnumerable&amp;lt;Type&amp;gt; types)
        {
            foreach (Type type in types)
            {
                if (String.Equals(type.FullName, name) || String.Equals(type.AssemblyQualifiedName, name))
                    return type;
            }
            return null;
        }

        /// &amp;lt;summary&amp;gt;
        /// CurrentDomainのアセンブリの型一覧を取得します。
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;returns&amp;gt;型一覧&amp;lt;/returns&amp;gt;
        public static IList&amp;lt;Type&amp;gt; GetCurrentDomainAssembliesType()
        {
            System.Reflection.Assembly[] assemblies;
            assemblies = System.AppDomain.CurrentDomain.GetAssemblies();
            List&amp;lt;Type&amp;gt; assemblyTypes = new List&amp;lt;Type&amp;gt;();
            foreach (System.Reflection.Assembly assembly in assemblies)
            {
                assemblyTypes.AddRange(assembly.GetTypes());
            }
            return assemblyTypes;
        }
    }
}
&lt;/pre&gt;
&lt;img src ="http://blogs.wankuma.com/hirase/aggbug/129691.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>T.Hirase</dc:creator><title>[C#] クロージャ？</title><link>http://blogs.wankuma.com/hirase/archive/2008/03/16/127990.aspx</link><pubDate>Sun, 16 Mar 2008 22:12:00 GMT</pubDate><guid>http://blogs.wankuma.com/hirase/archive/2008/03/16/127990.aspx</guid><wfw:comment>http://blogs.wankuma.com/hirase/comments/127990.aspx</wfw:comment><comments>http://blogs.wankuma.com/hirase/archive/2008/03/16/127990.aspx#Feedback</comments><slash:comments>6</slash:comments><wfw:commentRss>http://blogs.wankuma.com/hirase/comments/commentRss/127990.aspx</wfw:commentRss><trackback:ping>http://blogs.wankuma.com/hirase/services/trackbacks/127990.aspx</trackback:ping><description>&lt;p&gt;クロージャってのをよくわからず次のコードを「クロージャだと思って」コーディング。&lt;/p&gt;
&lt;pre class="csharp" xml:space="preserve" name="sourceCode"&gt;using System;

internal class PetitClosure
{
    private static void Main()
    {
        Func&amp;lt;int&amp;gt; f = CreateFunc();
        Console.WriteLine(f());
        Console.WriteLine(f());
        Console.WriteLine(f());
        Console.WriteLine(f());
    }

    private static Func&amp;lt;int&amp;gt; CreateFunc()
    {
        int i = 0;
        return delegate
            {
                i = i + 1;
                return i;
            };
    }
}&lt;/pre&gt;
&lt;p&gt;結果は、「1,2,3,4」・・・・・。えぇ・・・！　すご。&lt;/p&gt;
&lt;h2&gt;ポイント&lt;/h2&gt;
&lt;p&gt;ポイントは、CreateFunc関数で作られた関数 f を作ったときには、関数 f の戻り値が決定せず、関数 f が呼ばれるたびに CreateFunc関数内のスコープにある変数 i をインクリメントして戻してくれるってことですね。&lt;/p&gt;
&lt;p&gt;うー。まだまだ知らないことばっかだ。&lt;/p&gt;&lt;img src ="http://blogs.wankuma.com/hirase/aggbug/127990.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>T.Hirase</dc:creator><title>[C#] イベント関連の命名規則</title><link>http://blogs.wankuma.com/hirase/archive/2008/03/06/126541.aspx</link><pubDate>Thu, 06 Mar 2008 12:44:00 GMT</pubDate><guid>http://blogs.wankuma.com/hirase/archive/2008/03/06/126541.aspx</guid><wfw:comment>http://blogs.wankuma.com/hirase/comments/126541.aspx</wfw:comment><comments>http://blogs.wankuma.com/hirase/archive/2008/03/06/126541.aspx#Feedback</comments><slash:comments>12</slash:comments><wfw:commentRss>http://blogs.wankuma.com/hirase/comments/commentRss/126541.aspx</wfw:commentRss><trackback:ping>http://blogs.wankuma.com/hirase/services/trackbacks/126541.aspx</trackback:ping><description>&lt;p&gt;C#のイベント関連の命名規則ってあるのかな？&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;h2&gt;イベント名は、過去形で？&lt;/h2&gt; &lt;p&gt;イベント名は、どうやら「～～ed」と過去形になっていることが多い。&lt;/p&gt; &lt;p&gt;■1a. public event System.EventHandler ValueChanged;&lt;/p&gt; &lt;p&gt;でも名前だけを見たとき、イベントなのかプロパティなのかがわかりにくい。&lt;/p&gt; &lt;p&gt;■1b. public event System.EventHandler ValueChangedEvent;&lt;/p&gt; &lt;p&gt;だけど、これはこれで冗長な感じがする。&lt;/p&gt; &lt;h2&gt;そのイベントを起こす内部関数は、「On+イベント名」で？&lt;/h2&gt; &lt;p&gt;イベントを起こす際には1枚ラッパ関数を挟んで、イベントを起こすようだ。&lt;br&gt;で、そのラッパ関数は「On+イベント名」となっていることが多い。&lt;/p&gt; &lt;p&gt;■2a. protected void OnValueChanged() {}&lt;/p&gt; &lt;p&gt;これは、何となくイベントが「起きた」のか「起こす」のかがわからない。&lt;/p&gt; &lt;p&gt;■2b. protected void RaiseValueChanged() {}&lt;/p&gt; &lt;p&gt;これは、どうだろう。&lt;/p&gt; &lt;h2&gt;そのイベントを受け取る側は・・・？&lt;/h2&gt; &lt;p&gt;で、イベントを受け取る関数は、どう書いたらいいんだろう。&lt;br&gt;Visual Studio が吐くコードは、こんなの↓&lt;/p&gt; &lt;p&gt;■3a. void HogeClass_ValueChanged(object sender, EventArgs e){}&lt;/p&gt; &lt;p&gt;■3b. void myHogeInstance_ValueChanged(object sender, EventArgs e){}&lt;/p&gt; &lt;p&gt;だけど、これはそれぞれマイクロソフトの名前付け規則「&lt;a href="http://msdn2.microsoft.com/ja-jp/ms182245.aspx" target="_blank"&gt;識別子にはアンダースコアを使用できません&lt;/a&gt;」と「&lt;a href="http://msdn2.microsoft.com/ja-jp/ms182240.aspx" target="_blank"&gt;識別子の大文字/小文字を正しく使い分ける必要があります&lt;/a&gt;」に違反している。&lt;/p&gt; &lt;p&gt;で、考えたのは、以下。&lt;/p&gt; &lt;p&gt;■3c. void HogeClassValueChangedEventOccurred(object sender, EventArgs e) {}&lt;/p&gt; &lt;p&gt;これがはじめに考えたもの。ちょー長い。&lt;/p&gt; &lt;p&gt;■3d. void WhenValueChanged(object sender, EventArgs e) {}&lt;/p&gt; &lt;p&gt;これはわかりやすい（ような気がする）。&lt;br&gt;が、どこでイベントが起きたのかわからない。&lt;/p&gt; &lt;p&gt;■3e. void WhenValueChangedInHogeClass(object sender, EventArgs e) {}&lt;/p&gt; &lt;p&gt;そこで「In～～」と場所まで書く。&lt;br&gt;これは・・、長すぎて区切りがわからない。&lt;/p&gt; &lt;p&gt;■3f. void WhenHogeValueChanged(object sender, EventArgs e) {}&lt;/p&gt; &lt;p&gt;で、もう少し短くしてみる。&lt;br&gt;だけど、なんか意味をとりづらい気がする。&lt;/p&gt; &lt;p&gt;んー、どうしたら、いいんだろう。&lt;/p&gt;
&lt;h3&gt;追記 - 2008年3月6日 15:08@JST&lt;/h3&gt;
&lt;p&gt;フィードバック欄にて、よねけんさんが挙げておられるサイトは以下。&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://msdn.microsoft.com/library/ja/cpgenref/html/cpconeventnamingguidelines.asp"&gt;イベントの名前付けのガイドライン&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://msdn2.microsoft.com/ms229012.aspx"&gt;型のメンバの名前&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://msdn2.microsoft.com/ms229011.aspx"&gt;イベントのデザイン&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;追記 - 2008年3月7日 16:44@JST&lt;/h3&gt;
&lt;p&gt;フィードバック欄にて、れいさんが挙げておられるサイトは以下。&lt;/p&gt;
&lt;p&gt;msdnブログ、Krzysztof Cwalinaさんの&lt;a href="http://blogs.msdn.com/kcwalina/archive/2004/09/28/235232.aspx"&gt;「API Design Guidelines Digest」&lt;/a&gt;&lt;/p&gt;



&lt;img src ="http://blogs.wankuma.com/hirase/aggbug/126541.aspx" width = "1" height = "1" /&gt;</description></item></channel></rss>