从条件获取类型

时间:2017-01-11 06:22:26

标签: c# .net

下面是代码,其中我没有得到它包含的系统类型,因为我的If条件为True 如果条件结果为真,你可以帮我显示系统类型输入消息吗

 public static System.Type[] trackedRevisionsElements = new System.Type[] {
    typeof(CellDeletion),
    typeof(CellInsertion),
    typeof(CellMerge),
    typeof(CustomXmlDelRangeEnd),
    typeof(CustomXmlDelRangeStart),
    typeof(CustomXmlInsRangeEnd),
    typeof(CustomXmlInsRangeStart),
    typeof(Deleted),
    typeof(DeletedFieldCode),
    typeof(DeletedMathControl),
    typeof(DeletedRun),
    typeof(DeletedText),
    typeof(Inserted),
    typeof(InsertedMathControl),
    typeof(InsertedMathControl),
    typeof(InsertedRun),
    typeof(MoveFrom),
    typeof(MoveFromRangeEnd),
    typeof(MoveFromRangeStart),
    typeof(MoveTo),
    typeof(MoveToRangeEnd),
    typeof(MoveToRangeStart),
    typeof(MoveToRun),
    typeof(NumberingChange),
    typeof(ParagraphMarkRunPropertiesChange),
    typeof(ParagraphPropertiesChange),
    typeof(RunPropertiesChange),
    typeof(SectionPropertiesChange),
    typeof(TableCellPropertiesChange),
    typeof(TableGridChange),
    typeof(TablePropertiesChange),
    typeof(TablePropertyExceptionsChange),
    typeof(TableRowPropertiesChange),
};

    public static  bool PartHasTrackedRevisions(OpenXmlPart part)
    {
           //Check Whether part.Rootelment contains System Type or Not
            if (part.RootElement.Descendants()
            .Any(e => trackedRevisionsElements.Contains(e.GetType())))
        {
         //If True Show e.GetType In Message Box
        }
        return part.RootElement.Descendants()
            .Any(e => trackedRevisionsElements.Contains(e.GetType()));
    }

2 个答案:

答案 0 :(得分:1)

如果我正确理解您的要求,则可能会在比较中获得一个匹配项的更改,因为您将一个集合中的每个项目与另一个集合中的每个项目进行比较。所以希望你正在寻找这样的东西:

public static  bool PartHasTrackedRevisions(OpenXmlPart part)
{
     bool isFound = false;
     var typesFound = part.RootElement.Descendants()
                                      .Where(e => trackedRevisionsElements.Contains(e.GetType())).ToList();
    foreach(System.Type foundType in typesFound)
    {
        MessageBox.Show(foundType .ToString());
        isFound  = true;
    }
    return isFound; 
}

答案 1 :(得分:0)

您可以尝试使用FirstOrDefault代替Any

  Type result = part.RootElement
    .Descendants()
    .Select(e => e.GetType())
    .FirstOrDefault(e => trackedRevisionsElements.Contains(e));

  if (result != null) {   
    //TODO: Put the right message here
    MessageBox.Show(result.ToString());

    return true;
  }
  else 
    return false;