Guid mainfolderid = (main.GetValue(""));
其中main
是一个动态实体。
如何将上面提到的main.GetValue("")
转换为System.Guid
?
错误信息显示:
无法隐式转换对象类型为“System.Guid”。
Guid mainfolderid = (main.GetValue(""));
其中main
是一个动态实体。
如何将上面提到的main.GetValue("")
转换为System.Guid
?
错误信息显示:
无法隐式转换对象类型为“System.Guid”。
GetValue
方法是否返回以 object
类型为标识的 Guid
值?如果是这样,你只需要执行一个显式转换即可:
Guid mainfolderid = (Guid)main.GetValue("");
如果不是这样,GetValue
返回的内容是否可以被传递给其中一个构造函数(例如一个byte[]
或string
)?如果是这样,你可以尝试这样做:Guid mainfolderid = new Guid(main.GetValue(""));
如果以上两种情况都不适用,那么您需要手动转换通过 GetValue
返回的任何内容为 Guid
。Guid Guid.Parse(string input)
并且
bool Guid.TryParse(string input, out Guid result)
将做你想要的事情。
Guid mainfolderid = new Guid(main.GetValue("").ToString());
Guid mainfolderid = new Guid(main.GetValue("").ToString());
Guid mainfolderid = (Guid)(main.GetValue(""));
有一个解决方案是使用对象的类型:
GuidAttribute guidAttr;
object[] arrAttrs;
Type type;
string strGuid;
type = main.GetType();
arrAttrs = type.Assembly.GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), false);
guidAttr = (arrAttrs != null && arrAttrs.Length > 0) ? arrAttrs[0] as GuidAttribute: null;
if (guidAttr != null) {
strGuid = "{" + guidAttr.Value.ToUpper() + "}";
}
typeof(main)
的GUID,而不是每个main实例的唯一GUID,也不是由main.GetValue()
返回的每个值的唯一GUID。 - Kasper van den Berg