如何解析给定的文本中有哪些是类、属性、方法等

mcafee1984 2010-01-25 09:33:31
如题。例如:
public class Test
{
string m_sName = "test";
public void GetInfo()
{
}

public string Name
{
get{ return m_sName; }
}
}
如何通过代码获得类名是Test,方法名是GetInfo及属性是Name?求教!
...全文
185 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
mcafee1984 2010-01-27
  • 打赏
  • 举报
回复
不用动态编译,就是一般的文本解析,有什么例子吗?
qqiuzaihui 2010-01-26
  • 打赏
  • 举报
回复
貌似有签名匹配之类的东东,可以使用它去匹配类名,方法名及属性. 
是我在C#语言规范中看到的,我也没弄过,估计你得自己写一个类似的东东去查找相关信息.
我看的书是VS开发平台自带的: C# Language Specification 1.2 , 第10章 类. 
如果你装了VS开发平台,在类似的路径: C:\Program Files\Microsoft Visual Studio 9.0\VC#\Specifications\2052 下应该能找到. 
ztenv 2010-01-26
  • 打赏
  • 举报
回复
你要分析文本中的代码?然后取得类的信息?
saturn4263195 2010-01-26
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 problc 的回复:]
可以正则做文本解析,也可以用C#的动态编译,然后反射。
[/Quote]如上
mcafee1984 2010-01-26
  • 打赏
  • 举报
回复
给出的是文本,不是dll
aboluoyuren 2010-01-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wuyq11 的回复:]
Test t = new Test();
FieldInfo[] fields = t.GetType().GetFields(BindingFlags.NonPublic|BindingFlags.SetField|BindingFlags.Instance);

MethodInfo methodInfo = type.GetMethod("", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
[/Quote]

赞一个
khjian 2010-01-26
  • 打赏
  • 举报
回复
恩,反射
wanghui0380 2010-01-26
  • 打赏
  • 举报
回复
6楼可以

不过这东西有现成的可以用,通过BNF范式可以直接提出来

参考
http://www.codeplex.com/irony
tianliang1 2010-01-26
  • 打赏
  • 举报
回复
啊,高手何其多,来学习一下
cuike519 2010-01-26
  • 打赏
  • 举报
回复
六楼的方法不错,先使用动态编译的技术,然后再反射出来。

动态编译参考:
http://www.codeproject.com/KB/dotnet/DynamicCompileAndRun.aspx


或者建立一个简单的词法分析器,弄几个Token,分析一下也很容易。
Valefish 2010-01-25
  • 打赏
  • 举报
回复
Mark
江南小鱼 2010-01-25
  • 打赏
  • 举报
回复

static void ViewCls(object cls)
{
Type tp = cls.GetType();
Assembly assembly = tp.Assembly;
Type[] types = assembly.GetTypes();
foreach (Type type in types)
{
Console.WriteLine("类名:" + type.FullName);
//获取该类型的构造函数信息
ConstructorInfo[] myConstructors = type.GetConstructors();
Show(myConstructors);
//获取类型的字段信息
FieldInfo[] fields = type.GetFields();
Show(fields);
//获取方法信息
MethodInfo[] methods = type.GetMethods();
Show(methods);
//获取属性信息
PropertyInfo[] properties = type.GetProperties();
Show(properties);
}
}
static void Show(object[] objs)
{
foreach (object obj in objs)
{
Console.WriteLine(obj.ToString());
}
Console.WriteLine("---------------------");
}

wuyq11 2010-01-25
  • 打赏
  • 举报
回复
MethodInfo[] mis =t.GetType().GetMethods();
foreach (MethodInfo mi in mis)
{
Console.WriteLine(mi.Name);
}
@井九 2010-01-25
  • 打赏
  • 举报
回复
可以正则做文本解析,也可以用C#的动态编译,然后反射。
满衣兄 2010-01-25
  • 打赏
  • 举报
回复
winform的例子:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace WindowsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Type t = typeof(Form1);
MethodInfo[] info = t.GetMethods();
foreach (MethodInfo i in info)
{
MessageBox.Show(i.Name);
}

}

}
}


微软写的例子(这个例子在vs安装之后的目录里有):

//版权所有 (C) Microsoft Corporation。保留所有权利。

// AttributesTutorial.cs
// 本示例表明类属性和方法属性的用法。

using System;
using System.Reflection;
using System.Collections;

// IsTested 类是用户定义的自定义属性类。
// 它可以应用于任何声明,包括
// - 类型(结构、类、枚举、委托)
// - 成员(方法、字段、事、属性、索引器)
// 使用它时不带参数。
public class IsTestedAttribute : Attribute
{
public override string ToString()
{
return "Is Tested";
}
}

// AuthorAttribute 类是用户定义的属性类。
// 它只能应用于类和结构声明。
// 它采用一个未命名的字符串参数(作者的姓名)。
// 它有一个可选的命名参数 Version,其类型为 int。
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
public class AuthorAttribute : Attribute
{
// 此构造函数为属性类指定未命名的参数。
public AuthorAttribute(string name)
{
this.name = name;
this.version = 0;
}

// 此属性为只读(它没有 set 访问器)
// 因此,不能将其用作此属性的命名参数。
public string Name
{
get
{
return name;
}
}

// 此属性可读写(它有 set 访问器)
// 因此,将此类用作属性类时,
// 可以将其用作命名参数。
public int Version
{
get
{
return version;
}
set
{
version = value;
}
}

public override string ToString()
{
string value = "Author : " + Name;
if (version != 0)
{
value += " Version : " + Version.ToString();
}
return value;
}

private string name;
private int version;
}

// 此处,将用户定义的自定义属性 AuthorAttribute 附加
// 到 Account 类。创建属性时,会将未命名的
// 字符串参数传递到 AuthorAttribute 类的构造函数。
[Author("Joe Programmer")]
class Account
{
// 将自定义属性 IsTestedAttribute 附加到此方法。
[IsTested]
public void AddOrder(Order orderToAdd)
{
orders.Add(orderToAdd);
}

private ArrayList orders = new ArrayList();
}

// 将自定义属性 AuthorAttribute 和 IsTestedAttribute 附加
// 到此类。
// 请注意 AuthorAttribute 的命名参数“Version”的用法。
[Author("Jane Programmer", Version = 2), IsTested()]
class Order
{
// 在此处添加资料...
}

class MainClass
{
private static bool IsMemberTested(MemberInfo member)
{
foreach (object attribute in member.GetCustomAttributes(true))
{
if (attribute is IsTestedAttribute)
{
return true;
}
}
return false;
}

private static void DumpAttributes(MemberInfo member)
{
Console.WriteLine("Attributes for : " + member.Name);
foreach (object attribute in member.GetCustomAttributes(true))
{
Console.WriteLine(attribute);
}
}

public static void Main()
{
// 显示 Account 类的属性
DumpAttributes(typeof(Account));

// 显示已测试成员的列表
foreach (MethodInfo method in (typeof(Account)).GetMethods())
{
if (IsMemberTested(method))
{
Console.WriteLine("Member {0} is tested!", method.Name);
}
else
{
Console.WriteLine("Member {0} is NOT tested!", method.Name);
}
}
Console.WriteLine();

// 显示 Order 类的属性
DumpAttributes(typeof(Order));

// 显示 Order 类的方法的属性
foreach (MethodInfo method in (typeof(Order)).GetMethods())
{
if (IsMemberTested(method))
{
Console.WriteLine("Member {0} is tested!", method.Name);
}
else
{
Console.WriteLine("Member {0} is NOT tested!", method.Name);
}
}
Console.WriteLine();
}
}


其它例子:
http://topic.csdn.net/u/20071121/10/f3e97633-d156-4eda-9002-5e89513a2c22.html
满衣兄 2010-01-25
  • 打赏
  • 举报
回复
winform的例子:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace WindowsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Type t = typeof(Form1);
MethodInfo[] info = t.GetMethods();
foreach (MethodInfo i in info)
{
MessageBox.Show(i.Name);
}

}

}
}


微软写的例子(这个例子在vs安装之后的目录里有):

//版权所有 (C) Microsoft Corporation。保留所有权利。

// AttributesTutorial.cs
// 本示例表明类属性和方法属性的用法。

using System;
using System.Reflection;
using System.Collections;

// IsTested 类是用户定义的自定义属性类。
// 它可以应用于任何声明,包括
// - 类型(结构、类、枚举、委托)
// - 成员(方法、字段、事、属性、索引器)
// 使用它时不带参数。
public class IsTestedAttribute : Attribute
{
public override string ToString()
{
return "Is Tested";
}
}

// AuthorAttribute 类是用户定义的属性类。
// 它只能应用于类和结构声明。
// 它采用一个未命名的字符串参数(作者的姓名)。
// 它有一个可选的命名参数 Version,其类型为 int。
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
public class AuthorAttribute : Attribute
{
// 此构造函数为属性类指定未命名的参数。
public AuthorAttribute(string name)
{
this.name = name;
this.version = 0;
}

// 此属性为只读(它没有 set 访问器)
// 因此,不能将其用作此属性的命名参数。
public string Name
{
get
{
return name;
}
}

// 此属性可读写(它有 set 访问器)
// 因此,将此类用作属性类时,
// 可以将其用作命名参数。
public int Version
{
get
{
return version;
}
set
{
version = value;
}
}

public override string ToString()
{
string value = "Author : " + Name;
if (version != 0)
{
value += " Version : " + Version.ToString();
}
return value;
}

private string name;
private int version;
}

// 此处,将用户定义的自定义属性 AuthorAttribute 附加
// 到 Account 类。创建属性时,会将未命名的
// 字符串参数传递到 AuthorAttribute 类的构造函数。
[Author("Joe Programmer")]
class Account
{
// 将自定义属性 IsTestedAttribute 附加到此方法。
[IsTested]
public void AddOrder(Order orderToAdd)
{
orders.Add(orderToAdd);
}

private ArrayList orders = new ArrayList();
}

// 将自定义属性 AuthorAttribute 和 IsTestedAttribute 附加
// 到此类。
// 请注意 AuthorAttribute 的命名参数“Version”的用法。
[Author("Jane Programmer", Version = 2), IsTested()]
class Order
{
// 在此处添加资料...
}

class MainClass
{
private static bool IsMemberTested(MemberInfo member)
{
foreach (object attribute in member.GetCustomAttributes(true))
{
if (attribute is IsTestedAttribute)
{
return true;
}
}
return false;
}

private static void DumpAttributes(MemberInfo member)
{
Console.WriteLine("Attributes for : " + member.Name);
foreach (object attribute in member.GetCustomAttributes(true))
{
Console.WriteLine(attribute);
}
}

public static void Main()
{
// 显示 Account 类的属性
DumpAttributes(typeof(Account));

// 显示已测试成员的列表
foreach (MethodInfo method in (typeof(Account)).GetMethods())
{
if (IsMemberTested(method))
{
Console.WriteLine("Member {0} is tested!", method.Name);
}
else
{
Console.WriteLine("Member {0} is NOT tested!", method.Name);
}
}
Console.WriteLine();

// 显示 Order 类的属性
DumpAttributes(typeof(Order));

// 显示 Order 类的方法的属性
foreach (MethodInfo method in (typeof(Order)).GetMethods())
{
if (IsMemberTested(method))
{
Console.WriteLine("Member {0} is tested!", method.Name);
}
else
{
Console.WriteLine("Member {0} is NOT tested!", method.Name);
}
}
Console.WriteLine();
}
}


其它例子:
http://topic.csdn.net/u/20071121/10/f3e97633-d156-4eda-9002-5e89513a2c22.html
wuyq11 2010-01-25
  • 打赏
  • 举报
回复
Test t = new Test();
FieldInfo[] fields = t.GetType().GetFields(BindingFlags.NonPublic|BindingFlags.SetField|BindingFlags.Instance);

MethodInfo methodInfo = type.GetMethod("", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
diandian82 2010-01-25
  • 打赏
  • 举报
回复
搜索反射吧。有一大堆例子。
江南小鱼 2010-01-25
  • 打赏
  • 举报
回复
反射啊

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧