我目前正在努力创建一个公共属性,但是在调用构造函数时定义了类型。
这是我现在的代码:
public class clsHtml: IDisposable { public enum Types { Section, Input, File, Radio, Checkbox, Select }; public TYPE HERE element; public clsHtml(Types type) { CreateElement(type); } private void CreateElement(Types type) { switch(type) { case Types.Input: element = new HtmlInputText(); break; case Types.File: element = new HtmlInputFile(); break; case Types.Radio: element = new HtmlInputRadioButton(); break; case Types.Checkbox: element = new HtmlInputCheckBox(); break; case Types.Select: element = new HtmlSelect(); break; } } public override string ToString() { System.IO.StringWriter writer = new System.IO.StringWriter(); HtmlTextWriter html = new HtmlTextWriter(writer); element.RenderControl(html); return writer.ToString(); } }在哪里说TYPE HERE ,我应该使用dynamic还是object或什么? 我将从外部(公共)访问它,我只有一个简单的ToString()方法,它将使用StringWriter和HtmlTextWriter解析它以将其作为HTML字符串。
我正在使用ASP.NET(而不是MVC),所以我选择的是性能问题吗? 如果我选择object类型,我还可以访问switch内每个对象上的方法吗?
我想在这个类上做一个new定义它将创建的元素的类型,所以我可以使用所选类的一些方法来设置东西。 例如,如果我选择HtmlInputRadioButton我可以使用.Checked但我将在其他类中使用此元素,用于根据您创建的元素设置属性。 我清楚了吗?
一种可能的解决方案是使用每种类型创建变量,但是使用属性来查看哪一个不为null并获得该属性,但这不是一个优雅的解决方案。
其他解决方案是使用Generics使用where T : HtmlControls, new()但我希望用户不知道,我希望他只写new(Types.WHICH ONE TO USE) 。
谢谢大家!
I am currently struggling to create a public attribute but the type is defined when constructor is called.
This is my code now:
public class clsHtml: IDisposable { public enum Types { Section, Input, File, Radio, Checkbox, Select }; public TYPE HERE element; public clsHtml(Types type) { CreateElement(type); } private void CreateElement(Types type) { switch(type) { case Types.Input: element = new HtmlInputText(); break; case Types.File: element = new HtmlInputFile(); break; case Types.Radio: element = new HtmlInputRadioButton(); break; case Types.Checkbox: element = new HtmlInputCheckBox(); break; case Types.Select: element = new HtmlSelect(); break; } } public override string ToString() { System.IO.StringWriter writer = new System.IO.StringWriter(); HtmlTextWriter html = new HtmlTextWriter(writer); element.RenderControl(html); return writer.ToString(); } }Where it says TYPE HERE, should I use dynamic or object or what ? I am going to access it from outside (public), I just have a simple ToString() method that will parse it with StringWriter and HtmlTextWriter to get it as HTML String.
I am using ASP.NET (not MVC) so, is it a performance matter what I choose ? If I choose object type, can I still access the methods that are on each object inside the switch ?
I want to do a new on this class defining the type of element it will create, so I can use some methods of the selected class to set things. For example, if I choose HtmlInputRadioButton I can use .Checked but I will use this element in other class, for setting attributes depending which element you created. Am I clear ?
One possible solution is to create variables with each type, but use a property to see which one is not null and get that one but this is not an elegant solution.
Other solution is to use Generics using where T : HtmlControls, new() but I want the user to not know that, I want him to only write new(Types.WHICH ONE TO USE).
Thanks all !!!
最满意答案
public enum Types { Section, Input, File, Radio, Checkbox, Select }; // Why did you decide it to be IDisposable? What are you going to dispose? public class clsHtml: IDisposable { public HtmlControl element { get; private set; } public clsHtml(Types type) { this.CreateElement(type); } private void CreateElement(Types type) { switch(type) { case Types.Input: this.element = new HtmlInputText(); break; case Types.File: this.element = new HtmlInputFile(); break; case Types.Radio: this.element = new HtmlInputRadioButton(); break; case Types.Checkbox: this.element = new HtmlInputCheckBox(); break; case Types.Select: this.element = new HtmlSelect(); break; default: throw new NotImplementedException(type.ToString() + " not yet supported!"); } } public override string ToString() { System.IO.StringWriter writer = new System.IO.StringWriter(); HtmlTextWriter html = new HtmlTextWriter(writer); this.element.RenderControl(html); return writer.ToString(); } } var clsHtml = new clsHtml(Types.Input); // the following part sucks, but you don't want to use generics if (clsHtml.element is HtmlInputText) { HtmlInputText elementAsHtmlInputText = clsHtml.element as HtmlInputText; } else if ( ...) ... public enum Types { Section, Input, File, Radio, Checkbox, Select }; // Why did you decide it to be IDisposable? What are you going to dispose? public class clsHtml: IDisposable { public HtmlControl element { get; private set; } public clsHtml(Types type) { this.CreateElement(type); } private void CreateElement(Types type) { switch(type) { case Types.Input: this.element = new HtmlInputText(); break; case Types.File: this.element = new HtmlInputFile(); break; case Types.Radio: this.element = new HtmlInputRadioButton(); break; case Types.Checkbox: this.element = new HtmlInputCheckBox(); break; case Types.Select: this.element = new HtmlSelect(); break; default: throw new NotImplementedException(type.ToString() + " not yet supported!"); } } public override string ToString() { System.IO.StringWriter writer = new System.IO.StringWriter(); HtmlTextWriter html = new HtmlTextWriter(writer); this.element.RenderControl(html); return writer.ToString(); } } var clsHtml = new clsHtml(Types.Input); // the following part sucks, but you don't want to use generics if (clsHtml.element is HtmlInputText) { HtmlInputText elementAsHtmlInputText = clsHtml.element as HtmlInputText; } else if ( ...) ...更多推荐
发布评论