C#如何通过属性名称反射出属性本身

C#如何通过属性名称反射出属性本身,第1张

    public class ReflectDemo

    {

        public static void Main(string[] args)

        {

            // 正常方式创建对象和给属性赋值

            C c = new C();

            cN = "helloworld";

            // 正常方式赋值

            ConsoleWriteLine("正常方式赋值:");

            ConsoleWriteLine(cN);

            // 全反射方式

            var type = typeof(C);

            object obj = ActivatorCreateInstance(type);

            // 属性赋值            

            PropertyInfo pInfo = typeGetProperty("N");

            if (pInfo != null)

            {

                pInfoSetValue(obj, "helloworld", null);

                // 测试是否已经属性赋值。

                ConsoleWriteLine("测试是否已经属性赋值:");

                ConsoleWriteLine(((C)obj)N); 

                // 用反射方式获取属性的值

                ConsoleWriteLine("用反射方式获取属性的值:");

                ConsoleWriteLine(pInfoGetValue(obj, null)); 

            }

            ConsoleRead();

        }

    }

C类代码如下:

    public class C

    {

        private string n;

        public string N

        {

            get { return n; }

            set { n = value; }

        }

    }

你可以这么写:

class BodyImpl implements Body{

//do something

public static void main(String[] args) {

Type[] interfaces = BodyImplclassgetInterfaces();

ParameterizedType firstInterface = (ParameterizedType) interfaces[0];

Class c = (Class) firstInterfacegetActualTypeArguments()[0];

Systemoutprintln(cgetName()); // prints "AtomEntry"

}

}

就得到你所要的接口参数了!

Java代码

package ;

import java lang reflect Field;

import java lang reflect InvocationTargetException;

import java lang reflect Method;

public class TestBean {

private int age;

public static void main(String []args) throws InstantiationException IllegalAccessException SecurityException NoSuchFieldException{

try {

Class<> class =Class forName( TestBean );

Object tObject=class newInstance();

Field field=class getDeclaredField( age );

field setAccessible(true);  //设置私有属性范围

field set(tObject );

System out print(field get(tObject));

try {

Method method=class getMethod( setAge int class);

method invoke(tObject );

Method getMethod=class getMethod( getAge );

System out println(getMethod invoke(tObject));

} catch (NoSuchMethodException e) {

// TODO Auto generated catch block

e printStackTrace();

} catch (IllegalArgumentException e) {

// TODO Auto generated catch block

e printStackTrace();

} catch (InvocationTargetException e) {

// TODO Auto generated catch block

e printStackTrace();

}

} catch (ClassNotFoundException e) {

// TODO Auto generated catch block

e printStackTrace();

}

}

public int getAge() {

return age;

}

public void setAge(int age) {

this age = age;

}

lishixinzhi/Article/program/Java/hx/201311/26324

先获取Method对象

以下仅供参考

package comkiddtestzhidao;

import javalangreflectMethod;

/

  Hello world!

 

 /

public class Main {

    public static void main(String[] args) {

        Method method1 = null;

        Method method2 = null;

        try {

            method1 = ClassforName("comkiddtestzhidaoCat")getMethod("getName", (Class<>[]) null);

            method2 = ClassforName("comkiddtestzhidaoCat")getMethod("getChilds", (Class<>[]) null);

        } catch (NoSuchMethodException ex) {

            exprintStackTrace();

        } catch (SecurityException ex) {

            exprintStackTrace();

        } catch (ClassNotFoundException ex) {

            exprintStackTrace();

        }

        if (null != method1) {

            Systemoutprintln(method1getGenericReturnType()getTypeName());

        }

        if (null != method2) {

            Systemoutprintln(method2getGenericReturnType()getTypeName());

        }

    }

}

class Cat {

    private String name;

    private Cat[] childs;

    public String getName() {

        return name;

    }

    public void setName(String name) {

        thisname = name;

    }

    public Cat[] getChilds() {

        return childs;

    }

    public void setChilds(Cat[] childs) {

        thischilds = childs;

    }

}

以上就是关于C#如何通过属性名称反射出属性本身全部的内容,包括:C#如何通过属性名称反射出属性本身、如何利用java反射,获取属性接口的具体类、Java反射设置私有属性和获取属性等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9757619.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-01
下一篇2023-05-01

发表评论

登录后才能评论

评论列表(0条)

    保存