顯示具有 C# 標籤的文章。 顯示所有文章
顯示具有 C# 標籤的文章。 顯示所有文章

CJK characters identify sample


///
/// Ref:
/// http://en.wikipedia.org/wiki/CJK_Unified_Ideographs
/// http://blog.oasisfeng.com/2006/10/19/full-cjk-unicode-range/
/// http://www.unicode.org/ucd/filemoved.html
///

///
///
static public string CommontCJKIdentify(string text)
{
if (text.Any(c => c >= 0x3040 && c <= 0x309F))
{ return "J"; }
else if (text.Any(c => c >= 0X30A0 && c <= 0x30FF))
{ return "J"; }
else if (text.Any(c => c >= 0X31F0 && c <= 0x31FF))
{ return "J"; }
else if (text.Any(c => c >= 0XAC00 && c <= 0xD7AF))
{ return "K"; }
else if (text.Any(c => c >= 0X1100 && c <= 0x11FF))
{ return "K"; }
else if (text.Any(c => c >= 0X3130 && c <= 0x318F))
{ return "K"; }
else if (text.Any(c => c >= 20000 && c <= 0xFA2D))
{
return "C";
}
else
{ return "Others"; }
}

[Umbraco] Add more than one TinyMCE datatype on page or usercontrol

        private void addTinyMCEControl(Control targetPlaceHolder, TabPage tp)
{

DataTypeDefinition htmlEditorDataType =
DataTypeDefinition.GetDataTypeDefinition(new Guid("A521511A-7BEE-4C5B-8D7B-4B06B57392B3"));

//The other way can get datatype id directly.
//DataTypeDefinition htmlEditorDataType = DataTypeDefinition.GetDataTypeDefinition(-87);

TinyMCE umbracoTinyMCEEditor = (TinyMCE)htmlEditorDataType.DataType.DataEditor;

string toolbar_id = string.Empty;

if (umbracoTinyMCEEditor != null)
{
if (umbracoTinyMCEEditor.config["umbraco_toolbar_id"] != null)
umbracoTinyMCEEditor.config.Remove("umbraco_toolbar_id");
toolbar_id = "tinyMCEMenu_" + targetPlaceHolder.ClientID;
umbracoTinyMCEEditor.config.Add("umbraco_toolbar_id", toolbar_id);
targetPlaceHolder.Controls.Add(umbracoTinyMCEEditor);
tp.Menu.NewElement("div", toolbar_id, "tinymceMenuBar", 0);
}
}


Reference:


[LINQ] Get child controls


using System.Collections.Generic ;
using System.Linq;
///
/// Get all child & sub-child controls within a control by type
///

/// The control we're searching in form/page.
/// The control type we're looking for (i.e; TextBox)
///
public IEnumerable < Control > GetAllChildControls(Control control, Type type = null)
{
var controls = control.Controls.Cast< Control >();

if (type == null)
return controls.SelectMany(ctrl => GetAllChildControls(ctrl, type)).Concat(controls);
else
return controls.SelectMany(ctrl => GetAllChildControls(ctrl, type)).Concat(controls).Where(c => c.GetType() == type);
}



//How to use.
IEnumerable< Control > textBoxControls = GetAllChildControls(this, typeof(TextBox));
IEnumerable< Control > allConrols = GetAllChildControls(this);


Reference : http://www.dreamincode.net/code/snippet5634.htm

[Reflection] Get and Set values of Properties


object sampleObj = new object();
foreach (PropertyInfo info in sampleObj.GetType().GetProperties())
{
// To Get value
if (info.CanRead)
{
object o = info.GetValue(sampleObj, null);
}


// To Set value
object myValue = "Something";
if (info.CanWrite)
{
info.SetValue(sampleObj, myValue, null);
}
}

[Exception] Collection was modified; enumeration operation may not execute

Solution : replace for each loop to standard loop

 


ArrayList items = new ArrayList();

//foreach (var item in items)
//{
// //do something
//}

for (int i = 0; i < items.Count; i++)
{
//do something
}


Referece: http://alitarhini.wordpress.com/2011/04/06/collection-was-modified-enumeration-operation-may-not-execute/

.NET C# byte[] to String sample

Sample array & string

byte[] array = new byte[] { 0x48, 0x65, 0x6C, 0x6C, 0x6F, 
0x57, 0x6F, 0x72, 0x6C, 0x64 }; //"HelloWorld"

byte[] to String

string str = System.Text.Encoding.Default.GetString(array);
string str = System.Text.Encoding.ASCII.GetString(array);

byte[] to String

System.Text.ASCIIEncoding converter = new System.Text.ASCIIEncoding();
string str = converter.GetString(array);

HexString to byte[]

string str = ByteArrayToHex(array);
public static string ByteArrayToHex(byte[] array)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (byte element in array)
sb.Append(element.ToString("X2"));
return sb.ToString();
}

.NET C# String to byte[] sample

Sample array & string

byte[] array = new byte[] { 0x48, 0x65, 0x6C, 0x6C, 0x6F, 
0x57, 0x6F, 0x72, 0x6C, 0x64 }; //"HelloWorld"

String to byte[]

byte[] array= System.Text.Encoding.Default.GetBytes("HelloWorld");
byte[] array= System.Text.Encoding.ASCII.GetBytes("HelloWorld");

String to byte[]

System.Text.ASCIIEncoding converter = new System.Text.ASCIIEncoding();
byte[] array = converter.GetBytes("HelloWorld");

HexString to byte[]

byte[] array = HexToByteArray("48656C6C6F56F726C64");
private static byte[] HexToByteArray(string hexString)
{
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = System.Convert.ToByte(hexString.Substring(i * 2, 2), 16);
return returnBytes;
}

HexString to byte

byte element = HexToByte("48");
private static byte HexToByte(string hexString)
{
if (hexString.Length > 2 || hexString.Length <= 0)
throw new System.ArgumentException("hex must be 1 or 2 characters in length");
byte newByte = byte.Parse(hexString, System.Globalization.NumberStyles.HexNumber);
return newByte;
}

Console array Result

foreach (byte element in array)
{
System.Console.WriteLine("{0}\t{1}\t{2} ",
(char)element, element, element.ToString("X2"));

}
/*
H 72 48
e 101 65
l 108 6C
l 108 6C
o 111 6F
W 87 57
o 111 6F
r 114 72
l 108 6C
d 100 64
*/