2011-07-21 15 views
0

我注意到,微軟已經實施了CssTextWriter這是內部有.net的Css作家嗎?

internal sealed class CssTextWriter : TextWriter 
{ 
    .... 
} 

是否有一個CSS作家.NET是什麼?

例如,我想編寫代碼,如:

CssTextWriter writer = new CssTextWriter(textWriter); 
writer.WriteBeginCssRule("p"); 
writer.WriteAttribute("font-family", "Arial,Liberation Sans,DejaVu Sans,sans-serif"); 
writer.WriteEndCssRule(); 

上面的代碼將輸出到流如下:

p { font-family: Arial,Liberation Sans,DejaVu Sans,sans-serif; } 
+0

爲什麼? – leppie

+0

我正在開發一個在xml中定義的樣式表並將其轉換爲Css –

+3

編寫自己的代碼不應該花太長時間。 – CodesInChaos

回答

1

帶點 http://www.dotlesscss.org/看起來像它會做的工作,但有點多,我只是需要一個單一的clss

我打電話給內部微軟類(是淘氣,它可能會去未來版本的.net等....)

public class CssTextWriter 
{ 
    public CssTextWriter(TextWriter writer) 
    { 
     if (writer == null) 
     { 
      throw new ArgumentNullException("writer"); 
     } 

     this.Writer = writer; 
     this.Initialize(); 
    } 

    /// <summary> 
    /// Gets the writer. 
    /// </summary> 
    /// <value> 
    /// The writer. 
    /// </value> 
    public TextWriter Writer { get; private set; } 

    /// <summary> 
    /// Gets or sets the internal CSS text writer. 
    /// </summary> 
    /// <value> 
    /// The internal CSS text writer. 
    /// </value> 
    private object InternalCssTextWriter 
    { 
     get; 

     set; 
    } 

    public void WriteBeginCssRule(string selector) 
    { 
     this.InternalCssTextWriter.InvokeMethod("WriteBeginCssRule", new[] { selector }); 
    } 

    public void WriteEndCssRule() 
    { 
     this.InternalCssTextWriter.InvokeMethod("WriteEndCssRule"); 
    } 

    public void WriteAttribute(string name, string value) 
    { 
     this.InternalCssTextWriter.InvokeMethod("WriteAttribute", new[] { name, value }, new Type[] { typeof(string), typeof(string) }); 
    } 

    public void Write(string value) 
    { 
     this.InternalCssTextWriter.InvokeMethod("Write", new[] { value }, new Type[] { typeof(string) }); 
    } 

    public void WriteAttribute(HtmlTextWriterStyle key, string value) 
    { 
     this.InternalCssTextWriter.InvokeMethod("WriteAttribute", new object[] { key, value }, new Type[] { typeof(HtmlTextWriterStyle), typeof(string) }); 
    } 

    private void Initialize() 
    { 
     Type internalType = typeof(System.Web.UI.HtmlTextWriter).Assembly.GetType("System.Web.UI.CssTextWriter"); 
     ConstructorInfo ctor = internalType.GetConstructors(BindingFlags.Instance | BindingFlags.Public)[0]; 
     this.InternalCssTextWriter = ctor.Invoke(new[] { this.Writer }); 
    } 
}