我注意到Microsoft已经实现了一个内部的CssTextWriter
internal sealed class CssTextWriter : TextWriter
{
....
}
是否有针对.net的Css编写器?
例如,我想编写如下代码:
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 :(得分:1)
带点 http://www.dotlesscss.org/看起来它可以完成这项工作,但有点多我只需要一个clss
我把调用包装到了内部的Microsoft类中(是的顽皮,它可能会在以后的.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 });
}
}