将对象从 class 属性 返回到字典
Returning objects from a class property to a dictionary
我正在寻找一种方法将 class 中的 return 属性返回到字典中。让我举个例子...
如果您有这样的模型:
public class MyModelTemplate
{
string TestString { get; set; }
bool TestBool { get; set; }
}
并在 setter 中给它一些值:
public class TestClass
{
public MyModelTemplate Model_one
{
get { return Model_one; }
set { value.TestString = "I am a string"; value.TestBool = true; }
}
public MyModelTemplate Model_two
{
get { return Model_two; }
set { value.TestString = "I am a string TWO"; value.TestBool = false; }
}
}
是否可以制作一个函数并将项目放入字典中?
public static Dictionary<string, object> GetValuesFromModel(object model)
{
// Return the object from the model in a dictionary
return strReturnValues;
}
MyModelTemplate
中的属性必须是public。
你弄错了TestClass
。试试这个
public class TestClass
{
// Constructor
public TestClass ()
{
Model_one = new MyModelTemplate{
TestString = "I am a string", TestBool = true };
Model_two = new MyModelTemplate{
TestString = "I am a string TWO", TestBool = false };
}
public MyModelTemplate Model_one { get; set; }
public MyModelTemplate Model_two { get; set; }
}
用字典代替属性的解决方案:
public class TestClass
{
public Dictionary<string, MyModelTemplate> Models { get; } =
new Dictionary<string, MyModelTemplate>();
public TestClass()
{
Models.Add("Model_one", new MyModelTemplate{
TestString = "I am a string", TestBool = true });
Models.Add("Model_two", new MyModelTemplate{
TestString = "I am a string TWO", TestBool = false });
}
}
现在,您可以像这样访问字典
var test = new TestClass();
var model = test["Model_one"];
Console.WriteLine(model.TestString);
Console.WriteLine(model.TestBool);
在这种情况下,您可以使用 Reflection。请查看下面的代码 -
public static void Main()
{
var model1 = new MyModelTemplate() {TestString="A", TestBool = true};
foreach(var pair in GetValuesFromModel(model1)){
Console.WriteLine($"{pair.Key} - {pair.Value}");
}
}
public static Dictionary<string, object> GetValuesFromModel(object model)
{
var result = new Dictionary<string, object>();
var props = model.GetType().GetProperties();
foreach(var prop in props){
result.Add(prop.Name, (object)prop.GetValue(model));
}
return result;
}
它应该在控制台上为您提供以下输出 -
TestString - A
TestBool - True
我正在寻找一种方法将 class 中的 return 属性返回到字典中。让我举个例子...
如果您有这样的模型:
public class MyModelTemplate
{
string TestString { get; set; }
bool TestBool { get; set; }
}
并在 setter 中给它一些值:
public class TestClass
{
public MyModelTemplate Model_one
{
get { return Model_one; }
set { value.TestString = "I am a string"; value.TestBool = true; }
}
public MyModelTemplate Model_two
{
get { return Model_two; }
set { value.TestString = "I am a string TWO"; value.TestBool = false; }
}
}
是否可以制作一个函数并将项目放入字典中?
public static Dictionary<string, object> GetValuesFromModel(object model)
{
// Return the object from the model in a dictionary
return strReturnValues;
}
MyModelTemplate
中的属性必须是public。
你弄错了TestClass
。试试这个
public class TestClass
{
// Constructor
public TestClass ()
{
Model_one = new MyModelTemplate{
TestString = "I am a string", TestBool = true };
Model_two = new MyModelTemplate{
TestString = "I am a string TWO", TestBool = false };
}
public MyModelTemplate Model_one { get; set; }
public MyModelTemplate Model_two { get; set; }
}
用字典代替属性的解决方案:
public class TestClass
{
public Dictionary<string, MyModelTemplate> Models { get; } =
new Dictionary<string, MyModelTemplate>();
public TestClass()
{
Models.Add("Model_one", new MyModelTemplate{
TestString = "I am a string", TestBool = true });
Models.Add("Model_two", new MyModelTemplate{
TestString = "I am a string TWO", TestBool = false });
}
}
现在,您可以像这样访问字典
var test = new TestClass();
var model = test["Model_one"];
Console.WriteLine(model.TestString);
Console.WriteLine(model.TestBool);
在这种情况下,您可以使用 Reflection。请查看下面的代码 -
public static void Main()
{
var model1 = new MyModelTemplate() {TestString="A", TestBool = true};
foreach(var pair in GetValuesFromModel(model1)){
Console.WriteLine($"{pair.Key} - {pair.Value}");
}
}
public static Dictionary<string, object> GetValuesFromModel(object model)
{
var result = new Dictionary<string, object>();
var props = model.GetType().GetProperties();
foreach(var prop in props){
result.Add(prop.Name, (object)prop.GetValue(model));
}
return result;
}
它应该在控制台上为您提供以下输出 -
TestString - A
TestBool - True