Skip to content

Instantly share code, notes, and snippets.

View lmatt-bit's full-sized avatar
🎱

lmatt lmatt-bit

🎱
View GitHub Profile
@lmatt-bit
lmatt-bit / gist:5302231
Created April 3, 2013 15:27
Html Agility Pack Usage Example
using HtmlAgilityPack;
//Download http://somapage
WebClient wc = new WebClient();
var page = wc.DownloadString("http://somepage");
//use Html Agility Pack to load Html
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(page);
@lmatt-bit
lmatt-bit / WebDownload.cs
Last active December 17, 2015 05:48
WebClient Timeout setting
public class WebDownload : WebClient
{
private int _timeout;
/// <summary>
/// 超时时间(毫秒)
/// </summary>
public int Timeout
{
get
{
@lmatt-bit
lmatt-bit / gist:9930262
Created April 2, 2014 08:38
How to apply xlst
XPathDocument myXPathDoc = new XPathDocument(myXmlFile) ;
XslCompiledTransform myXslTrans = new XslCompiledTransform();
myXslTrans.Load(myStyleSheet);
XmlTextWriter myWriter = new XmlTextWriter("result.html",null);
myXslTrans.Transform(myXPathDoc,null,myWriter);
@lmatt-bit
lmatt-bit / gist:9949743
Created April 3, 2014 07:20
Serialize object to xml(C# version)
using System;
public class clsPerson
{
public string FirstName;
public string MI;
public string LastName;
}
class class1
@lmatt-bit
lmatt-bit / gist:10111431
Created April 8, 2014 11:22
Get all members for a class
Test instance = new Test();
Type type = typeof(Test);
Dictionary<string, object> properties = new Dictionary<string, object>();
foreach (PropertyInfo prop in type.GetProperties())
properties.Add(prop.Name, prop.GetValue(instance));
@lmatt-bit
lmatt-bit / gist:10111810
Created April 8, 2014 11:28
Accessing Attributes by Using Reflection
// Multiuse attribute.
[System.AttributeUsage(System.AttributeTargets.Class |
System.AttributeTargets.Struct,
AllowMultiple = true) // Multiuse attribute.
]
public class Author : System.Attribute
{
string name;
public double version;
@lmatt-bit
lmatt-bit / gist:10257251
Created April 9, 2014 11:25
Find derived types for a type
public static IEnumerable<Type> FindDerivedInterfaceTypes(Type type)
{
return System.Reflection.Assembly.GetAssembly(type).GetTypes().Where(t => t.GetInterfaces().Contains(type));
}
@lmatt-bit
lmatt-bit / gist:10257526
Created April 9, 2014 11:27
Call generic template method from name rather than template
Type type = SchemaAssemblyUtility.GetType(schemaName);
var method = typeof(SchemaContext).GetMethod("CreateModel");
var genericMethod = method.MakeGenericMethod(type);//Here use type
return genericMethod.Invoke(schemaContext, null);
@lmatt-bit
lmatt-bit / gist:10257630
Created April 9, 2014 11:28
Test if a type is IList<>(generic type)
public static bool IsList(Type type)
{
return type != null && type.GetGenericTypeDefinition() == typeof(IList<>);
}
@lmatt-bit
lmatt-bit / gist:10258062
Created April 9, 2014 11:33
Right click on treeview to trigger contextmenu
private void treeView_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Right)
{
return;
}
TreeViewHitTestInfo info = treeView.HitTest(e.Location);
if (info.Node == null)