gpt4 book ai didi

c# - 奇数 "Use of possibly unassigned field ' 类型'"- CS0170

转载 作者:太空宇宙 更新时间:2023-11-03 14:09:46 27 4
gpt4 key购买 nike

我可能遗漏了一些东西,但我得到了一个我认为似乎很奇怪的错误,没有其他开发人员使用相同的代码...

public void SomeMethod(... symbolInfo)
{
ElementId elementId = symbolInfo.GetElementIds().Head(true);
if (elementId.HasValue())
{
// error here "Use of possibly unassigned field 'Type'"
object element = repository.FindElement(elementId.Type, elementId.Id);
if (element != null) { ... }
}
}

public struct ElementId
{
public string Id;
public MDAPI_ElementType Type;
}

使用以下扩展方法:

public static bool IsEmpty(this ElementId id)
{
return id.Type == ElementType.ElementUnknown || string.IsNullOrEmpty(id.Id);
}

public static bool HasValue(this ElementId id)
{
return !id.IsEmpty();
}

谁能告诉我为什么这不会构建?

最佳答案

我通过调用直接采用 ElementId 的扩展方法设法修复了我的构建错误。我不知道为什么这可以解决问题!

public static object FindElement(this IMRepository rep, ElementId element)
{
return rep.FindElement(element.Type, element.Id);
}

关于c# - 奇数 "Use of possibly unassigned field ' 类型'"- CS0170,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8225819/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com