在本文中,我们将带你了解c#–为什么属性执行比Field或Method执行慢?在这篇文章中,我们将为您详细介绍c#–为什么属性执行比Field或Method执行慢?的方方面面,并解答c#属性值无效常见
在本文中,我们将带你了解c# – 为什么属性执行比Field或Method执行慢?在这篇文章中,我们将为您详细介绍c# – 为什么属性执行比Field或Method执行慢?的方方面面,并解答c#属性值无效常见的疑惑,同时我们还将给您一些技巧,以帮助您实现更有效的.net – 为什么TextFieldParser.ReadField从字段中间删除连续的换行符?、asp.net – 在Gridview中有条件地隐藏CommandField或ButtonField、Class.getDeclaredFields()和Class.getFields()的区别。 Class.getMethods()和Class.getDeclaredMethods()的区别。、Django datefield和timefield到python datetime。
本文目录一览:- c# – 为什么属性执行比Field或Method执行慢?(c#属性值无效)
- .net – 为什么TextFieldParser.ReadField从字段中间删除连续的换行符?
- asp.net – 在Gridview中有条件地隐藏CommandField或ButtonField
- Class.getDeclaredFields()和Class.getFields()的区别。 Class.getMethods()和Class.getDeclaredMethods()的区别。
- Django datefield和timefield到python datetime
c# – 为什么属性执行比Field或Method执行慢?(c#属性值无效)
A property method can take a long time to execute; field access always
completes immediately. A common reason to use properties is to
perform thread synchroni- zation,which can stop the thread forever,
and therefore,a property should not be used if thread
synchronization is required. In that situation,a method is preferred.
Also,if your class can be accessed remotely (for example,your class
is derived from System.MarshalByRefObject),calling the property
method will be very slow,and therefore,a method is preferred to a
property. In my opinion,classes derived from MarshalByRefObject
should never use properties.
即使属性被定义为只返回私有字段也是如此?为什么同步方法首选?为什么在MarshalByRefObject方案中首选一个方法?
澄清我的问题:Jeff似乎正在做一个全面的陈述,即属性是不可取的,在这两种情况下,方法是比较可取的.正如Joe White指出的,属性可以有任意代码.但方法可以运行相同的任意代码.这是我有困难的部分.使用方法实际上是有利于使用属性(给定相同的代码用于同步还是编组),还是仅仅使用语言约定的问题?
解决方法
如果所有的属性都返回一个字段,那么它的方法体实际上将被JIT编译器内联,它将和一个字段访问一样快.所以不是这样的东西慢慢的;这是他们是黑盒子.如果你不知道一个财产是如何实现的,那么你不能对它快速返回做出假设.
(也就是说,制造一个缓慢的财产将会明显违反.NET Framework Design Guidelines,特别是这一个:“使用一种方法而不是一种财产,[如果]操作比一个字段设置的速度要慢一个数量级. )
至于他提出使用方法的建议,我不能理解.属性是方法:属性getter是一个方法(通常命名为get_PropertyName),属性setter是一个方法(set_PropertyName),读取属性的代码被编译为使得方法调用get_PropertyName的代码.没有什么特别的,使一个属性比方法更慢.
.net – 为什么TextFieldParser.ReadField从字段中间删除连续的换行符?
这是我正在阅读的一个示例文件,只有一个字段.引号是文件内容的一部分,有三个换行符(包括第2行后的两个连续换行符):
"This is line 1 This is line 2 This is line 4,which follows two consecutive newlines."
这是我用来解析和读取文件的代码:
Dim reader as New Microsoft.VisualBasic.FileIO.textfieldparser(myFile,System.Text.Encoding.Default) reader.TextFieldType = FileIO.FieldType.Delimited reader.SetDelimiters(",") Dim fields As String() = reader.ReadFields Dim line As String = fields(0)
这是“行”变量的内容.请注意,现在只有两个新行:
This is line 1 This is line 2 This is line 4,which follows two consecutive newlines.
我该怎么做才能保留连续的换行符?
If ReadFields encounters blank lines,
they are skipped and the next
non-blank line is returned.
我相信你需要做的是使用ReadLine http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser.readline.aspx然后循环结果.
Using MyReader As New Microsoft.VisualBasic.FileIO.textfieldparser("C:\ParserText.txt") MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited MyReader.Delimiters = New String() {","} Dim currentRow As String While Not MyReader.EndOfData Try currentRow = MyReader.ReadLine() 'Manipulate line... Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException MsgBox("Line " & ex.Message & " is invalid. Skipping") End Try End While End Using
asp.net – 在Gridview中有条件地隐藏CommandField或ButtonField
这样做最好的方法是什么?我更喜欢一个程序化的声明式解决方案。
解决方法
<asp:GridView runat="server" ID="GV1" AutoGenerateColumns="false"> <Columns> <asp:BoundField datafield="Name" HeaderText="Name" /> <asp:BoundField datafield="Age" HeaderText="Age" /> <asp:TemplateField> <ItemTemplate> <asp:Button runat="server" Text="Reject" Visible='<%# IsOverAgeLimit((Decimal)Eval("Age")) %>' CommandName="Select"/> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
然后,在代码背后,添加方法:
protected Boolean IsOverAgeLimit(Decimal Age) { return Age > 35M; }
这里的优点是可以很容易地测试IsOverAgeLimit方法。
Class.getDeclaredFields()和Class.getFields()的区别。 Class.getMethods()和Class.getDeclaredMethods()的区别。
package www.cn.extend;
/** Animal
* 2019/07/04
* @author Administrator
*
*/
public class Animal {
public String mName;
public String mSex;
private String mSecret = "I hava a secret but I can''t tell you know";
public String getSecret() {
return mSecret;
}
private void privateMethod() {
System.out.println("just a privateMethod");
}
}
package www.cn.extend;
/**
* Dog
* @author Administrator
*
*/
public class Dog extends Animal{
private String mJob;
public String mSkill;
protected String mDrive;
int mAge;
void keepFit(){
System.out.println(Dog.class.getSimpleName()+" to keepFit");
}
private void sooEsad(){
System.out.println("asdf");
}
}
package www.cn.extend;
import java.lang.reflect.Method;
public class Test {
public static void main(String[] args){
/**.class.getFields();
* 返回父类和子类中公共属性的Field数组。
*/
// Field[] fields = Dog.class.getFields();
// for (Field field:fields){
// System.out.println("this is a field from "+Dog.class.getSimpleName() +" : "+field);
// }
/**.class.getDeclaredFields();
* 返回子类中所有访问权限的属性。
*/
// Field[] declaredFields = Dog.class.getDeclaredFields();
// for (Field field:declaredFields){
// System.out.println("this is a declaredFields from "+Dog.class.getSimpleName() +" : "+field);
// }
/**.class.getMethods();
* 返回父类的公共方法
*/
// Method[] methods = Dog.class.getMethods();
// for (Method method:methods){
// System.out.println("this is a methods from "+Dog.class.getSimpleName() +" : "+method);
// }
/**.class.getDeclaredMethods();
* 返回子类所有权限的方法。
*/
Method[] declaredMethods = Dog.class.getDeclaredMethods();
for (Method method:declaredMethods){
System.out.println(declaredMethods[0].getName());
System.out.println("this is a declaredMethods from "+Dog.class.getSimpleName() +" : "+method);
}
}
}
Django datefield和timefield到python datetime
我有一个带有事件的单独Datefield和Timefield的Django模型。有没有一种方法可以将其转换为python
datetime对象,以便我可以精确地查询即将发生的事件?目前,我只知道第二天的到来。
models.py
event_time = models.TimeField()
event_date = models.DateField()
基本上,我可以过滤一分钟,甚至一秒钟吗?
谢谢。
今天关于c# – 为什么属性执行比Field或Method执行慢?和c#属性值无效的分享就到这里,希望大家有所收获,若想了解更多关于.net – 为什么TextFieldParser.ReadField从字段中间删除连续的换行符?、asp.net – 在Gridview中有条件地隐藏CommandField或ButtonField、Class.getDeclaredFields()和Class.getFields()的区别。 Class.getMethods()和Class.getDeclaredMethods()的区别。、Django datefield和timefield到python datetime等相关知识,可以在本站进行查询。
本文标签: