2015年7月19日 星期日

[C#]取得類別成員的屬性

參考
How do I get the custom attribute value of a field? [duplicate]

以取得字串最大長度為例子,取得成員的StringLength屬性

程式碼如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;

namespace RetriveClassAttribute
{
    class Program
    {
        static void Main(string[] args)
        {

            foreach (var prop in typeof(RetriveClassAttribute).GetProperties())
            {
                var attrs = (StringLengthAttribute[])prop.GetCustomAttributes
                    (typeof(StringLengthAttribute), false);
                foreach (var attr in attrs)
                {
                    Console.WriteLine("{0}: {1}", prop.Name, attr.MaximumLength);
                }
            }

            Console.ReadLine();
        }

    }
    class RetriveClassAttribute
    {
        [StringLength(30)]
        public string FirstMember { get; set; }
    }
}




2015年7月15日 星期三

[C#]WCF Service 傳遞XML Data

參考
逐步解說:建立和存取 WCF 服務

WCF服務程式庫與WCF服務應用程式的不同

WCF服務應用程式原始碼

ISendXmlWcfService.svc
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Xml.Linq;

namespace SendXmlWcfService
{
    public class SendXmlWcfService : ISendXmlWcfService
    {
        public string GetData(string value)
        {
            XDocument xml = XDocument.Parse(value);

            return string.Format("To {0}\r\n    {1}\r\n  {2} {3}", xml.Root.Element("to").Value, xml.Root.Element("body").Value,xml.Root.Element("heading").Value,xml.Root.Element("from").Value);
        }
    }
}

ISendXmlWcfService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace SendXmlWcfService
{
    [ServiceContract]
    public interface ISendXmlWcfService
    {
        [OperationContract]
        string GetData(string value);
    }
}

測試WCF服務應用程式-主控台應用程式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder builder = new StringBuilder("<note>" +
                                                      "<to>Tove</to>" +
                                                      "<from>Jani</from>" +
                                                      "<heading>Reminder</heading>" +
                                                      "<body>Don't forget me this weekend!</body>" +
                                                      "</note>");
            ServiceReference1.SendXmlWcfServiceClient client = new ServiceReference1.SendXmlWcfServiceClient();

            Console.WriteLine(client.GetData(builder.ToString()));

            Console.ReadLine();

        }
    }
}