18 Ekim 2011 Salı

String.RemoveMany (Extension Method)

public static string RemoveMany(this string dirtyString, params string[] stringsToRemove)
{
            return dirtyString.Split(stringsToRemove, StringSplitOptions.None)
           .Aggregate((sentence, next) => sentence + next)
}

Kullanımı:
string dirty = "This is ABCa st)r(ing.";
string dirty2 = "tDEFes.t-";
string[] delims = new string[] { "ABC", "(", ")" };
Console.WriteLine(dirty.RemoveMany(delims));
Console.WriteLine(dirty2.RemoveMany("DEF", ".", "-"));
//output
"This is a string."
"test"

14 Ekim 2011 Cuma

Sql Data Source Enumerator Extensions

Mevcut ağ üzerindeki tüm kullanılabilir SQL sunucu örnekleri bir liste halinde alan method örneği.

 public static IEnumerable<string> GetAvailableSqlServers(this SqlDataSourceEnumerator sqlDataSourceEnumerator)
        {
            return from row in SqlDataSourceEnumerator.Instance.GetDataSources().AsEnumerable()
                   orderby row.Field<string>("ServerName"), row.Field<string>("InstanceName")
                   select string.Concat(
                        row.Field<string>("ServerName"),
                        row.Field<string>("InstanceName") != string.Empty
                            ? string.Concat("\\", row.Field<string>("InstanceName"))
                            : string.Empty);
        }

HTML Document Extensions Methods

        public static void AddScript(this HtmlDocument htmlDocument, string javaScript)
        {
            HtmlElement head = htmlDocument.GetElementsByTagName("head")[0];
            HtmlElement scriptElement = htmlDocument.CreateElement("script");
            IHTMLScriptElement element = (IHTMLScriptElement)scriptElement.DomElement;
            element.text = javaScript;
            head.AppendChild(scriptElement);
        }

        public static void AddCSS(this HtmlDocument htmlDocument, string cssFileName)
        {
            IHTMLDocument2 currentDocument = (IHTMLDocument2)htmlDocument.DomDocument;
            int length = currentDocument.styleSheets.length;
            IHTMLStyleSheet styleSheet = currentDocument.createStyleSheet(@"", length + 1);
            using (TextReader reader = new StreamReader(cssFileName))
            {
                styleSheet.cssText = reader.ReadToEnd();
            }
        }

        public static void DoPostBack(this HtmlDocument document)
        {
            document.InvokeScript("__doPostBack");
        }

        public static void DoPostBack(this HtmlDocument document, string eventTarget, string eventArgument)
        {
            document.InvokeScript("__doPostBack", new object[] { eventTarget, eventArgument });
        }

        public static HtmlElement GetElementByTitle(this HtmlDocument document, string title)
        {
            return (from x in document.All.Cast<HtmlElement>()
                    where x.GetAttribute("title") == title
                    select x).SingleOrDefault();
        }