[英]How to pass Object in as string list in c# controller?
public class abcController : ApiController
{
public IEnumerable<string> Get()
{
return new string[] { "A","B","C"};
}
}
我想在字符串數組中傳遞包含名稱、id 和圖像的 object。如何做到這一點? 請幫助我.. 還告訴我如何訪問此 object 中的任何特定元素..
我想這就是你要找的。 我已經把它放在控制台應用程序中。 您可以將邏輯復制到您的 controller
public class ImageInformation
{
public ImageInformation(string name, string id, string image)
{
Name = name;
Id = id;
Image = image;
}
public string Name { get; private set; }
public string Id { get; private set; }
public string Image { get; private set; }
}
public static IEnumerable<ImageInformation> Get()
{
return new List<ImageInformation> {
new ImageInformation("A", "1", "A.jpg"),
new ImageInformation("B", "2", "B.jpg"),
new ImageInformation("C", "3", "C.jpg"),
};
}
public static void Main()
{
var images = Get();
var imageA = images.First(x => x.Name == "A");
var imageAName = imageA.Name;
var imageAId = imageA.Id;
var imageAImage = imageA.Image;
Console.WriteLine(imageAName);
Console.WriteLine(imageAId);
Console.WriteLine(imageAImage);
}
您可以做的是使用 XML 序列化。
What XML Serializing does is it converts an object to XML format (which is a string) and then when you want to get the object back you can use XML Deserializer to Deserialize the XML string format to the original object.
例如,您的 object 可以是包含字符串名稱、int ID、字符串 imagePath 的 class,我們稱之為 class 人。
[System.Serializable]
public class person
{
public string name { get; set; }
public int id { get; set; }
public string imagePath { get; set; }
}
using System.Xml.Serialization;
XML_Manager class 我們將把我們的方法放在:-
public static class XML_Manager
{
// The Serializing Method
public static string serializeObject<T>(T toSerialize)
{
// Create the result string variable
string result = "";
// Create a new StringWriter that will carry the serialization and dispose it as soon as it's done it's job
using(StringWriter sw = new StringWriter())
{
// Create the serializer that will serialize the object of type (T)
XmlSerializer xs = new XmlSerializer(typeof(T));
// Serialize the object and put the result on the StringWriter (sw)
xs.Serialize(sw, toSerialize);
// Exctract the result from the StringWriter (sw) by converting it to string
result = sw.ToString();
}
// Return the result
return result;
}
// The Deserializing Method
public static T deserializeObject<T>(string XmlString)
{
// Create the result variable of type (T)
T Result;
// Create a new StringReader that will carry the XmlString and dispose it as soon as it's done it's job
using (StringReader sr = new StringReader(XmlString))
{
// Create the serializer that will deserialize the object of type (T)
XmlSerializer xs = new XmlSerializer(typeof(T));
// Deserialize the XmlString and extract the object
Result = (T)xs.Deserialize(sr);
}
// Return the result
return Result;
}
}
// Create a new person
person p = new person();
// Set the name
p.name = "John";
// Set the ID
p.id = 2345;
// Set the Image Path
p.imagePath = "desktop";
// the xml serialized string
string xmlString = XML_Manager.serializeObject(p);
// the deserialized person
person deserializePerson = XML_Manager.deserializeObject<person>(xmlString);
// Show the serialized string
MessageBox.Show(xmlString);
// Show the deserialized object
MessageBox.Show($"Name: {deserializePerson.name}, Id: {deserializePerson.id}, Image Path: {deserializePerson.imagePath}");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.