sinson123 2018-3-22 11:37
我個程式想簡化用for去將資料assign返個class嘅property[code]ArrayList pptyList = new ArrayList();
Object[] ppty = { this.ID, this.Name, this.Actor, this.Serial, this.Publisher, this.Tag, this.Category }; pptyList.AddRange(ppty);[/code]因為要移除某個property,所以放咗入arraylist[code] for (int i = 0; i < indexs.Length - 1; i++) { if (indexs[i] > -1) { pptyList[i] = stringValue.Substring(indexs[i], indexs[i + 1] - indexs[i]); } else { continue; } }[/code]我想用一個for去放返入去,但出黎最後係放咗入pptylist,本身ppty依個array嘅object就無assign到value比佢
有無得byref咁,可以用for去loop佢,又放得返個array嘅ref 嘅object呢???
sinson123 2018-4-5 06:29
[quote]原帖由 [i]煙民母親生賤種[/i] 於 2018-3-22 10:08 PM 發表 [url=http://computer.discuss.com.hk/redirect.php?goto=findpost&pid=477284732&ptid=27325800][img]http://computer.discuss.com.hk/images/common/back.gif[/img][/url]
1. 唔洗 > Array.length - 1
2. Break 唔係 continue
3. Arraylist variable 放外圍, pass parameters 時加 ref. [/quote]
1.咁咪outofbound囉 :smile_45:
2.我後便重有其他要check,break唔到喎 :smile_27:
3.Arraylist 已經係外圍,ref 亦試過:smile_44:
最後我都係loop多次用switch
第一點已經足夠比豬你 :smile_45::smile_45:
Susan﹏汪汪 2018-4-5 15:01
你講的assign翻落object本身的properties
係要用C# reflections 的功能
汪汪冇電腦寫C#做示範
只有用swift寫類似的比你睇
[attach]8109445[/attach]
marlin12 2018-4-5 15:18
[quote]原帖由 [i]Susan﹏汪汪[/i] 於 2018-4-5 03:01 PM 發表 [url=http://computer.discuss.com.hk/redirect.php?goto=findpost&pid=478013795&ptid=27325800][img]http://computer.discuss.com.hk/images/common/back.gif[/img][/url]
汪汪冇電腦寫C#做示範[/quote]
[url=https://dotnetfiddle.net/].net fiddle[/url]
Susan﹏汪汪 2018-4-5 23:20
[quote]原帖由 [i]marlin12[/i] 於 2018-4-5 03:18 PM 發表 [url=http://www.discuss.com.hk/redirect.php?goto=findpost&pid=478014371&ptid=27325800][img]http://www.discuss.com.hk/images/common/back.gif[/img][/url]
.net fiddle [/quote]
用C#寫好似有D複雜
聽日有時間先
[url=https://stackoverflow.com/questions/12970353/c-sharp-dynamically-set-property?noredirect=1&lq=1]https://stackoverflow.com/questions/12970353/c-sharp-dynamically-set-property?noredirect=1&lq=1[/url]
Susan﹏汪汪 2018-4-6 09:44
[quote]原帖由 [i]marlin12[/i] 於 2018-4-5 03:18 PM 發表 [url=http://computer.discuss.com.hk/redirect.php?goto=findpost&pid=478014371&ptid=27325800][img]http://computer.discuss.com.hk/images/common/back.gif[/img][/url]
.net fiddle [/quote][code]using System;
using System.Reflection;
public class Program
{
public class Foo
{
public string str1 { get; set; }
public string str2 { get; set; }
}
public static void Main()
{
Foo foo = new Foo();
Console.WriteLine(foo.str1);
Console.WriteLine(foo.str2);
PropertyInfo[] pptyList = { foo.GetType().GetProperty("str1"), foo.GetType().GetProperty("str2") };
foreach(PropertyInfo ppty in pptyList)
{
if (ppty != null)
{
ppty.SetValue(foo, "Hello", null);
}
}
Console.WriteLine(foo.str1);
Console.WriteLine(foo.str2);
}
}[/code]
sswroom 2018-4-6 10:17
[quote]原帖由 [i]sinson123[/i] 於 2018-3-22 11:37 AM 發表 [url=http://computer.discuss.com.hk/redirect.php?goto=findpost&pid=477252253&ptid=27325800][img]http://computer.discuss.com.hk/images/common/back.gif[/img][/url]
我個程式想簡化用for去將資料assign返個class嘅propertyArrayList pptyList = new ArrayList();
Object[] ppty = { this.ID, this.Name, this.Actor, this.Serial, this.Publisher, this.Tag, this.Category }; ... [/quote]
用Reflection
大約的用法:
System.Reflection.FieldInfo fi = this.GetType().GetField("ID");
pptyList.Add(fi);
val = fi.GetValue(this);
fi.SetValue(this, val);
sinson123 2018-4-8 19:04
[quote]原帖由 [i]Susan﹏汪汪[/i] 於 2018-4-6 09:44 AM 發表 [url=http://computer.discuss.com.hk/redirect.php?goto=findpost&pid=478052916&ptid=27325800][img]http://computer.discuss.com.hk/images/common/back.gif[/img][/url]
using System;
using System.Reflection;
public class Program
{
public class Foo
{
public string str1 { get; set; }
public string str2 { get; set; }
}
public static void Mai ... [/quote]
完全諗唔到:smile_40::smile_o12::smile_o12::smile_o12:
form5 2018-4-9 20:14
[quote]原帖由 [i]sinson123[/i] 於 2018-3-22 11:37 AM 發表 [url=http://computer.discuss.com.hk/redirect.php?goto=findpost&pid=477252253&ptid=27325800][img]http://computer.discuss.com.hk/images/common/back.gif[/img][/url]
我個程式想簡化用for去將資料assign返個class嘅propertyArrayList pptyList = new ArrayList();
Object[] ppty = { this.ID, this.Name, this.Actor, this.Serial, this.Publisher, this.Tag, this.Category }; ... [/quote]
Property cast object 再𨍭 array , 神奇之code
睇吾到有需要用reflection,可能而加興亂咁嚟
sinson123 2018-4-10 16:39
[quote]原帖由 [i]form5[/i] 於 2018-4-9 08:14 PM 發表 [url=http://computer.discuss.com.hk/redirect.php?goto=findpost&pid=478258309&ptid=27325800][img]http://computer.discuss.com.hk/images/common/back.gif[/img][/url]
Property cast object 再𨍭 array , 神奇之code
睇吾到有需要用reflection,可能而加興亂咁嚟 [/quote]
直覺覺得我有條string拆返資料出黎放入返去個object嘅property,最直接用loop 入去會最簡單:lol
最後發現個array入便有嘅嘢係唔會ref返本身個object度,object還object,array還array
:smile_45::smile_45::smile_45: