获取自定义资源和自定义键将字符串转换为 "ResourceManager"
Get custom resource and custom key converting string to "ResourceManager"
我正在尝试在 ASP.NET 中获取用于翻译的资源,使用字符串转换为类型 ResourceManager 和用于翻译的外部资源文件。但是当我执行时,我看到了这个错误:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 24: Type myProp = element.GetProperty("ResourceManager").GetType();
Line 25: MethodInfo getString = myProp.GetMethod("GetString");
Line 26: var r = getString.Invoke(this, new object[] { key });
Line 27: return r.ToString();
Line 28: }
这是我的代码:
protected string getText(string key)
{
Type element = Type.GetType($"Website.Language.{translation}.general");
Type myProp = element.GetProperty("ResourceManager").GetType();
MethodInfo getString = myProp.GetMethod("GetString");
var r = getString.Invoke(this, new object[] { key });
return r.ToString();
}
显然,getString
变量returns为null,但是“GetString”方法应该调用ResourceManager的“GetString”方法。
有人可以帮我解决这个问题吗?
MethodInfo.Invoke
的第一个参数必须是某个对象的实例(据我所知,它需要与您反映的类型相同,在您的情况下Website.Language.{translation}.general
) .
这可能行不通,但只是为了向您展示您需要前进的方向。
protected string getText(string key)
{
Type element = Type.GetType($"Website.Language.{translation}.general");
var instance = Activator.CreateInstance(element);
Type myProp = element.GetProperty("ResourceManager").GetType();
MethodInfo getString = myProp.GetMethod("GetString");
var r = getString.Invoke(instance, new object[] { key });
return r.ToString();
}
另请参阅 this 示例。
我正在尝试在 ASP.NET 中获取用于翻译的资源,使用字符串转换为类型 ResourceManager 和用于翻译的外部资源文件。但是当我执行时,我看到了这个错误:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 24: Type myProp = element.GetProperty("ResourceManager").GetType();
Line 25: MethodInfo getString = myProp.GetMethod("GetString");
Line 26: var r = getString.Invoke(this, new object[] { key });
Line 27: return r.ToString();
Line 28: }
这是我的代码:
protected string getText(string key)
{
Type element = Type.GetType($"Website.Language.{translation}.general");
Type myProp = element.GetProperty("ResourceManager").GetType();
MethodInfo getString = myProp.GetMethod("GetString");
var r = getString.Invoke(this, new object[] { key });
return r.ToString();
}
显然,getString
变量returns为null,但是“GetString”方法应该调用ResourceManager的“GetString”方法。
有人可以帮我解决这个问题吗?
MethodInfo.Invoke
的第一个参数必须是某个对象的实例(据我所知,它需要与您反映的类型相同,在您的情况下Website.Language.{translation}.general
) .
这可能行不通,但只是为了向您展示您需要前进的方向。
protected string getText(string key)
{
Type element = Type.GetType($"Website.Language.{translation}.general");
var instance = Activator.CreateInstance(element);
Type myProp = element.GetProperty("ResourceManager").GetType();
MethodInfo getString = myProp.GetMethod("GetString");
var r = getString.Invoke(instance, new object[] { key });
return r.ToString();
}
另请参阅 this 示例。