作为变量的属性名称
我需要找到数据库中每个字符串列的最大大小作为设计另一个数据库的信息之一。我对源数据库的唯一访问是通过 Web 服务。我可以对许多列中的每一列执行此操作以找到最大尺寸,但我希望它是通用的,以便稍后使用它。
我编写了这个非常简化的版本,以使其易于理解。最后两行发明了语法,这就是我需要帮助的地方。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class myClass
{
private string s;
public string S
{
get { return s; }
set { s = value; }
}
private int i;
public int I
{
get { return i; }
set { i = value; }
}
}
class Program
{
static void Main(string[] args)
{
Type myClassType = typeof(myClass);
System.Reflection.PropertyInfo[] propertyInfo = myClassType.GetProperties();
Dictionary<string, int> property = new Dictionary<string, int>();
foreach (System.Reflection.PropertyInfo info in propertyInfo)
if (info.PropertyType == typeof(System.String))
property.Add(info.Name, -1);
myClass[] myPa = new myClass[2];
myPa[0] = new myClass();
myPa[0].S = "1";
myPa[0].I = 0;
myPa[1] = new myClass();
myPa[1].S = "12";
myPa[1].I = 1;
这就是我需要帮助的地方。我发明了c[pair.key]
。如何引用我知道名称的属性?
foreach (myClass c in myPa)
foreach (KeyValuePair<string, int> pair in property)
if (c[pair.Key].Length > pair.Value)
property[pair.Key] = c[pair.Key].Length;
foreach (KeyValuePair<string, int> pair in property)
Console.WriteLine("Property: {0}, Biggest Size: {1}", pair.Key, pair.Value);
}
}
}
输出应该是:
Property: S Biggest Size: 2
I need to find the biggest size of each string column in a database as one of the informations to design another database. The only access I have to the source database is by a web service. I can just do it for each of the many columns to find the biggest size but I want it generic so I can use it later.
I wrote this very simplified version to make it simple to understand. Two of the last lines have invented syntax and it is where I need help.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class myClass
{
private string s;
public string S
{
get { return s; }
set { s = value; }
}
private int i;
public int I
{
get { return i; }
set { i = value; }
}
}
class Program
{
static void Main(string[] args)
{
Type myClassType = typeof(myClass);
System.Reflection.PropertyInfo[] propertyInfo = myClassType.GetProperties();
Dictionary<string, int> property = new Dictionary<string, int>();
foreach (System.Reflection.PropertyInfo info in propertyInfo)
if (info.PropertyType == typeof(System.String))
property.Add(info.Name, -1);
myClass[] myPa = new myClass[2];
myPa[0] = new myClass();
myPa[0].S = "1";
myPa[0].I = 0;
myPa[1] = new myClass();
myPa[1].S = "12";
myPa[1].I = 1;
This is where I need help. I invented the c[pair.key]
. How to have a reference to a property that I know the name of?
foreach (myClass c in myPa)
foreach (KeyValuePair<string, int> pair in property)
if (c[pair.Key].Length > pair.Value)
property[pair.Key] = c[pair.Key].Length;
foreach (KeyValuePair<string, int> pair in property)
Console.WriteLine("Property: {0}, Biggest Size: {1}", pair.Key, pair.Value);
}
}
}
Output shoud be:
Property: S Biggest Size: 2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下内容应该足够了:
尽管您可能会在“GetProperties”结果集上添加一些额外的过滤器(例如取出静态过滤器或索引属性等)。
它使用了几个 Linq 扩展函数,即“Where”, “GroupBy”、“SelectMany”、“Select”和“Max”以及使用匿名类型。
The following should suffice:
Though you might add some extra filters on the "GetProperties" result set (such as taking out static ones, or indexing properties, etc.
It makes use of a couple of Linq extension functions, namely, "Where", "GroupBy", "SelectMany", "Select", and "Max" as well using anonymous types.
不太确定我是否理解正确,但是:
如果只在类中使用索引属性 this[propertyname] ,它内部将像对象一样返回指定属性的值,该怎么办?
这意味着,恐怕你不能只写
c[pair.key].Length
,因为Object
没有Length
属性。您需要将其转换为所需的类型(在您的情况下为string
),并且仅在使用Length
属性之后。如果这不是您所要求的,请重新提出您的问题。
Not very sure if I understood right, but:
What if just use indexed property in your class this[propertyname] which inside will return a value of specified property like an object.
Which means, I'm afraid, you can not just write
c[pair.key].Length
, asObject
does not haveLength
property. You need to cast it to desired type (string
in your case) and only after useLength
property.If it's not what you were asking for, please refrase your question.