【问题标题】:How to get Class propertyName to create lable?如何获取类属性名称来创建标签?
【发布时间】:2011-03-10 03:54:10
【问题描述】:

我如何学习在 asp.net 中创建页面表单的属性


class MyClass
{
    public int id { get; set;}
    public string Name { get; set;}
    public string Name { get; set;}
}

ForExample :(以编程方式生成 Asp.net 表单)

 void SetRecursiveTextBoxAndLabels()
        {

            Label lbl;
                 MyClass eng = new MyClass();
                Type typ = eng.GetType();
                PropertyInfo[] properties = typ.GetProperties();
                for( int i =0;  i<properties.Length; i++)
                {
                    lbl = new Label();
                    lbl.ID = properties[i].Name;
                    lbl.Text = properties[i].Name;
                    PlaceHolder1.Controls.Add(lbl);
                }
            }
        }

但它运行不正确:(加载太慢了...顺便说一句,我没有实时创建类。我正在使用 linqtoSql 。您可以谈论基于 linqtosql 的解决方案...

【问题讨论】:

  • 您提供的代码应该运行得比较快。你给我们提供了一个缩短的版本吗? SetRecursiveTextBoxAndLabels() 会被多次调用吗?

标签: c# .net asp.net visual-studio-2008


【解决方案1】:

看看Cache Reflection Results (Class Properties) 看看缓存你的反射调用是否会有所帮助。正如GenericTypeTea 建议的那样,多次调用它可能是您看到的任何延迟的原因。这里还有一个指向Fast Reflection Library 的链接,这可能是让其他人为您实现和调试缓存代码的一种方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-13
    • 2010-10-14
    • 2019-08-13
    • 1970-01-01
    • 2010-09-14
    • 2019-04-04
    • 2013-05-11
    • 1970-01-01
    相关资源
    最近更新 更多