C#访问泛型类T的属性

时间:2016-08-23 17:56:39

标签: c# generics

我需要访问泛型Class T的属性 我在类泛型

中有这个方法
08-23 18:35:21.867 15308-15308/com.app.danieltavares.teste E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: com.app.danieltavares.teste, PID: 15308
                                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.danieltavares.teste/com.app.danieltavares.teste.MainActivity}: android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class fragment
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                                             at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                             at android.os.Looper.loop(Looper.java:154)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                                          Caused by: android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class fragment
                                                                          Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class fragment
                                                                          Caused by: java.lang.IllegalArgumentException: Binary XML file line #16: Must specify unique android:id, android:tag, or have a parent with an id for com.app.danieltavares.teste.TestFragment
                                                                             at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2282)
                                                                             at android.app.FragmentController.onCreateView(FragmentController.java:98)
                                                                             at android.app.Activity.onCreateView(Activity.java:5884)
                                                                             at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:34)
                                                                             at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:80)
                                                                             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:777)
                                                                             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
                                                                             at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
                                                                             at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
                                                                             at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
                                                                             at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136)
                                                                             at com.app.danieltavares.teste.MainActivity.onCreate(MainActivity.java:11)
                                                                             at android.app.Activity.performCreate(Activity.java:6664)
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                                             at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                             at android.os.Looper.loop(Looper.java:154)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

1 个答案:

答案 0 :(得分:1)

尝试以下代码。

public T calcuste(T obj)
{
    calcaulte testobj= new calcaulte ();
    calcaulte obj_calcaulte  = obj as calcaulte;
    if(obj_calcaulte  != null)
    {
        testobj.Id = obj_calcaulte  .Id;   
    }

}   

您需要控制null,因为obj可能为null或可能属于不同的类。