利用swagger建立互動式API文件
2015年12月28日 星期一
最後附近github位址:https://github.com/domaindrivendev/Swashbuckle Read more...
using System;
public class GetNameTest {
enum Colors { Red, Green, Blue, Yellow };
enum Styles { Plaid, Striped, Tartan, Corduroy };
public static void Main() {
Console.WriteLine("The 4th value of the Colors Enum is {0}", Enum.GetName(typeof(Colors), 3));
Console.WriteLine("The 4th value of the Styles Enum is {0}", Enum.GetName(typeof(Styles), 3));
}
}
public static class HelpExtensions
{
public static string GetEnumDescription(this Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
//若取不到屬性,則取名稱
if ((attributes != null) && (attributes.Length > 0))
return attributes[0].Description;
else
return value.ToString();
}
}
public enum MessageType
{
[Description("Create")]
Creation = 1,
[Description("Update")]
Updating = 2,
[Description("Del")]
Deletion = 3,
[Description("Select")]
Selection = 4
}
//使用方式
var messageType = MessageType.Deletion.GetEnumDescription();
Console.WriteLine("Ouput:" + messageType);
//顯示結果
//Output:Del
Read more...
查詢指令
csc /help
編譯cs檔方式
C:\Windows\Microsoft.NET\Framework\v4.0.30319>csc /t:exe /out:c:\helloworld\hell
oworld.exe c:\helloworld\helloworld.cs
public class HelloWorld
{
public static void Main(string[] args)
{
System.Console.WriteLine("Hello World!");
System.Console.ReadLine();
}
}
今天遇到使用Guid.NewGuid在IE8的奇怪問題
如果在網站列按Enter則會發生,但按F5重新整理確不會
最後確定是瀏覽器Cache問題造成的,我來重現一下實際的情況
測試程式如下:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Guid.NewGuid().ToString() );
}
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetNoStore(); //清除Cache
Response.Write(Guid.NewGuid().ToString() );
}
Read more...
public class SiteMapMenu : HierarchicalDataBoundControl
{
public void RecursiveCreateChildControls(IHierarchicalEnumerable dataItems, bool isRecursiveCall)
{
//抓取自訂屬性
System.Web.SiteMapNode node = dataItem.Item as System.Web.SiteMapNode;
bool customerTag = node["coustomerFlag"];
if(customerTag) continue;
}
}
//Web.sitemap
(CreationStatus)Enum.Parse(typeof(CreationStatus), dataReader["CreationStatus"].ToString());
private static ListRead more...AddECEventLog(IDataReader dataReader) { var ecEventLogList = new List (); while (dataReader.Read()) { var ecEventLog = new ECEventLog(); ecEventLog.ECEventId = dataReader["ECEventId"] == DBNull.Value ? null : dataReader["ECEventId"].ToString(); ecEventLog.CreationStatus = (CreationStatus)Enum.Parse(typeof(CreationStatus), dataReader["CreationStatus"].ToString()); ecEventLogList.Add(ecEventLog); } return ecEventLogList; }
Func & Action是委派用法,差別在於Func:有回傳值,Action:不需要回傳值
首先來看一下委派基本用法
//委派基本定義
public delegate string BaseDeletegate(string source);
public static class ServiceBase
{
public static string Execute(BaseDeletegate action, string source)
{
string value = action.Invoke(source);
return value;
}
}
//呼叫委派用法
public static void runBaseDeletegate()
{
BaseDeletegate action = ResponseMsg;
string result = ServiceBase.Execute(action, "Neil");
Console.WriteLine(result);
Console.ReadLine();
}
//執行的方法
public static string ResponseMsg(string str)
{
return "Hello:" + str;
}
執行結果
//Func定義
public static string ExecuteFunc(Func action)
{
return action(); //有回傳值
}
//呼叫Func方法
public static void runBaseFunc()
{
string result = ServiceBase.ExecuteFunc(() => {
return ResponseMsg("FuncNeil");
});
Console.WriteLine(result);
Console.ReadLine();
}
//執行的方法
public static string ResponseMsg(string str)
{
return "Hello:" + str;
}
//Action定義
public static void ExecuteAction(Action action)
{
action(); //沒有回傳值
}
//呼叫Action方法
public static void runBaseAction()
{
ServiceBase.ExecuteAction(() => {
ActionResopnseMsg("ActionNeil");
});
}
//執行的方法
public static void ActionResopnseMsg(string str)
{
Console.WriteLine("Hello:" + str);
Console.ReadLine();
}
寫了這麼久的程式,都沒特別注意到這二者的差別
簡單來說throw 會多一項原始執行錯誤行號。
throw ex resets the stack trace (so your errors would appear to originate from HandleException)throw doesn't - the original offender would be preserved.
static void Main(string[] args) {
try {
ThrowException1(); // line 19
} catch (Exception x) {
Console.WriteLine("Exception 1:");
Console.WriteLine(x.StackTrace);
}
try {
ThrowException2(); // line 25
} catch (Exception x) {
Console.WriteLine("Exception 2:");
Console.WriteLine(x.StackTrace);
}
}
private static void ThrowException1() {
try {
DivByZero(); // line 34
} catch {
throw; // line 36
}
}
private static void ThrowException2() {
try {
DivByZero(); // line 41
} catch (Exception ex) {
throw ex; // line 43
}
}
private static void DivByZero() {
int x = 0;
int y = 1 / x; // line 49
}
最近遇到一個操作集合物件時,需要做群組的功能,以前都是在SQL裡操作
今天透過Linq的Group By方法來實作,順便記錄一下。
![]() |
| 測試資料結構 |
![]() |
| Group By後結果 |
//放入測試資料 var testList = new List(); testList.Add("event1,s11,1"); testList.Add("event1,s12,1"); testList.Add("event1,s13,1"); testList.Add("event1,s14,1"); testList.Add("event1,s15,1"); testList.Add("event2,s21,1"); testList.Add("event2,s22,1"); //處理資料格式 var sourceList = new List >(); foreach (var item in testList) { var querySplit = item.Split(','); string eventId = querySplit[0]; string selectionId = querySplit[1]; string handicap = querySplit[2]; var list = new KeyValuePair (eventId, string.Format("{0}#{1}", selectionId, handicap)); sourceList.Add(list); } //利用Group By 放入Dictionary物件裡 Dictionary > mList = sourceList.GroupBy(p => p.Key,p=>p.Value) .ToDictionary(p => p.Key, p => p.Select(s => s) .ToList());
常常會搞不太清楚傳值以及傳址的差異,就先來筆記一下,以免以後忘記
範例如下:
static void Main(string[] args)
{
//參考型別傳值(兩個變數位址不同)
TestClass y = new TestClass();
TestClass r1 = ChangeByVal(y);
Console.WriteLine("r1和y指向同實體:" + (r1 == y).ToString());
//參考型別傳址(兩個變數位址相同)
TestClass r2 = ChangeByRef(ref y);
Console.WriteLine("r2和y指向同實體:" + (r2 == y).ToString());
Console.ReadLine();
}
private static TestClass ChangeByVal(TestClass y)
{
y = new TestClass();
return y;
}
private static TestClass ChangeByRef(ref TestClass y)
{
y = new TestClass();
return y;
}
補充:這張圖可以說明兩個的不同處
Read more...var 可用在暱名型別上,強型別或稱右(後)決議型別,只能做為宣告區域變數使用,一定要定義右邊的型態
編譯時期才做轉型
var x = new object();
//編譯時
object x = new object();
範例如下:
Read more...
//使用var的場合
static void main(string[] args)
{
string[] words = { "apple","cherry","blueberry"};
var newwords = words.select(w => new { upper = w.toupper(), lower = w.tolower() });
foreach (var item in newwords)
{
console.writeline(item.lower + ":" + item.upper);
}
console.readkey();
}