小编Pet*_*ter的帖子

查看是否在使用反射的方法内调用方法

我正在使用反射,目前有一个MethodBody.如何检查MethodBody中是否调用了特定方法?

Assembly assembly = Assembly.Load("Module1");
Type type = assembly.GetType("Module1.ModuleInit");
MethodInfo mi = type.GetMethod("Initialize");
MethodBody mb = mi.GetMethodBody();
Run Code Online (Sandbox Code Playgroud)

.net c# reflection

9
推荐指数
2
解决办法
5094
查看次数

XElement仅添加前缀

我有一个XML文件,如:

<myPrefix:Catalog xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
 xmlns:sys="clr-namespace:System;assembly=mscorlib" 
 xmlns:myPrefix="clr-namespace:........">

  <myPrefix:Item Name="Item1" Mode="All" />
  <myPrefix:Item Name="Item2" Mode="Single" />

</myPrefix:Catalog>
Run Code Online (Sandbox Code Playgroud)

使用C#我创建一个新项目,如:

XContainer container = XElement.Parse(xml);
XElement xmlTree = 
   new XElement("Item",
      new XAttribute("Name", item.Name),
      new XAttribute("Mode", item.Mode));
Run Code Online (Sandbox Code Playgroud)

如您所见,我不添加"myPrefix"前缀.谁能告诉我怎么能这样做?我不想再声明xmlns.谢谢,彼得

.net c# xml linq-to-xml

7
推荐指数
2
解决办法
6390
查看次数

例外或If/Else语句

我有一个关于何时使用Exception或If/Else语句的问题.在我的情况下,我想检查DocumentNode是否是TestNode.当它是TestNode时,我想获得节点.

我在下面写了两个可能的解决方案.第一个解决方案认为它是TestNode,否则它会产生异常.第二个解决方案检查它是否是TestNode,然后它执行大致相同的函数来获取节点.谁能告诉我什么是最好的解决方案?或者有更好的解决方案吗?谢谢,彼得.

*对不起,我的英语不好..

private DocumentNode GetTestNode(IEnumerable<DocumentNode> nodes)
{
    foreach (DocumentNode node in nodes)
    {
        if (node.GetValueProperty().ToString() == "TestValue")
        {
            return node;
        }
    }
    throw new TestNodeNotFoundException("This is not a TestNode");
}

OR: 

private DocumentNode IsTestNode(IEnumerable<DocumentNode> nodes) 
{
    foreach (DocumentNode node in nodes)
    {
        if (node.GetValueProperty().ToString() == "TestValue")
        {
            return true;
        }
    }
    return false;
}

private DocumentNode GetTestNode(IEnumerable<DocumentNode> nodes)
{
    foreach (DocumentNode node in nodes)
    {
        if (node.GetValueProperty().ToString() == "TestValue")
        {
            return node;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

.net c# if-statement exception

2
推荐指数
1
解决办法
1126
查看次数

标签 统计

.net ×3

c# ×3

exception ×1

if-statement ×1

linq-to-xml ×1

reflection ×1

xml ×1