var task = Task.Run(...)
try
{
task.Wait(30000); // Rethrows any exception(s).
...
reference : https://stackoverflow.com/questions/32067034/how-to-handle-task-run-exception
var task = Task.Run(...)
try
{
task.Wait(30000); // Rethrows any exception(s).
...
1: public static class StringExtension
2: {
3: // 字串反轉
4: public static string Reverse(string s)
5: {
6: if (String.IsNullOrEmpty(s))
7: return "";
8: char[] charArray = new char[s.Length];
9: int len = s.Length - 1;
10: for (int i = 0; i <= len; i++)
11: {
12: charArray[i] = s[len - i];
13: }
14: return new string(charArray);
15: }
16: }
1: class Program
2: {
3: static void Main(string[] args)
4: {
5: string s = "123456789";
6: Console.WriteLine(StringExtension.Reverse(s));
7: }
8: }
6: Console.WriteLine(s.Reverse());
4: public static string Reverse(this string s)
1: public static class DateTimeExt
2: {
3: // 將 DateTime 物件格式化成中華民國年份的日期字串.
4: public static string ToRocDateString(DateTime date, char separator)
5: {
6: int year = (date.Year - 1911);
7: return year.ToString() + separator + date.Month + separator + date.Day;
8: }
9: }
4: public static string ToRocDateString(this DateTime date, char separator)
Console.WriteLine(DateTime.Now.ToRocDate('/'));
你不用擔心呼叫擴充方法時會忘記要傳入哪些參數,Visual Studuo 的 IntelliSense 功能會提示你。ProductDTO product = ((IList<ProductDTO>)Products)[0];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public class Person
{
public string Name { get; set; }
public string Surname { get; set; }
public int ID { get; set; }
private Person()
{
}
public Person(string name, string surname, int id)
{
this.Name = name;
this.Surname = surname;
this.ID = id;
}
}
|
InvalidOperationException
exception (cannot be serialized because it does not have a parameterless constructor) if you try to serialize a class without one. Luckily you can just set it to private or internal so it won’t be accessible from other classes.
1
2
3
4
5
6
7
8
9
|
Person aPerson = new Person("name", "surname", 1); // The Person object which we will serialize
string serializedData = string.Empty; // The string variable that will hold the serialized data
XmlSerializer serializer = new XmlSerializer(aPerson.GetType());
using (StringWriter sw = new StringWriter())
{
serializer.Serialize(sw, aPerson);
serializedData = sw.ToString();
}
|
serializedData
variable will be holding the serialized data. This is what you will use when storing or transmitting your object somewhere. You can see below the contents of serializedData
.
1
2
3
4
5
6
|
<?xml version="1.0" encoding="utf-16"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>name</Name>
<Surname>surname</Surname>
<ID>1</ID>
</Person>
|
1
2
3
4
5
|
XmlSerializer deserializer = new XmlSerializer(typeof(Person));
using (TextReader tr = new StringReader(serializedData))
{
Person deserializedPerson = (Person)deserializer.Deserialize(tr);
}
|
deserializedPerson
object will have exactly the same data as the object we serialized at the start.