파라미터로 넘오온 typeof(Class명) 관련 질문드립니다.

아래와 같이 사용할 때 오류없이 잘 작동합니다.
그런데 아래 invoke 할 때 첫번째 인자 T 에서 Exception 오류가 나옵니다.
Target Object가 매칭이 안된다 하고 말입니다.
Docs 원본에는 파라미터로 T 를 받아온것은 아니고 메소드 안데서 강제로
ClassType T = new ClassType(); 이렇게 되있거든요…
그래서 Type T = new T(); 이렇게 해봤는데 예외오류가 나옵니다.
저는 파라미터로 (정 안되면 방식이 되야 하는데 제 능력으로는 invoke에서
오류가 안나오게 할 수 없더라고요. ㅠㅠ

class TestClass 
{
    void Test(Type T)
    {
         PropertyInfo[] propInfos   = T.GetProperties();
         // . 잘 됩니다.
         // .. 중략

         if (method.ReturnType != typeof(void))  // not setter
            Console.WriteLine(">>>" + method.Invoke(typeof(T), new object[] {}));
         }            
    }
}
1개의 좋아요

using TEST.Helper;

using System;

using System.IO;

using System.Text;

using System;

using System.Threading;

using System.Threading.Tasks;

using System.ServiceProcess;

using System.Diagnostics;

using System.Reflection;

namespace Test

{

    public class VoClass

    {

        public string ID        { get; set; }   = "1";

        public string NM        { get; set; }   = "2";

        public string TYPE      { get; set; }   = "3";

        public string APP_PATH  { get; set; }   = "4";

        public string EXE_FILE  { get; set; }   = "5";

        public string EXE_PARAM { get; set; }   = "6";

        public string APP_TYPE  { get; set; }   = "7";

        public ushort NOT_SAFE  { get; set; }  //2022.05.10 추가

        public VoClass()

        {}

    }

    public class Vardump

    {

        public void PropertyGet(Type T)

        {

           

            PropertyInfo[] propInfos   = T.GetProperties();

            //Console.WriteLine(T + ", " + propInfos.Length );

            for(int i=0; i<propInfos.Length; i++)

            {

                PropertyInfo info = propInfos[i];

                MethodInfo[] methods = info.GetAccessors(); // getter , setter

                for(int j = 0; j < methods.Length; j++)

                {

                    MethodInfo method = methods[j];

                    if (method.ReturnType != typeof(void))  // not setter

                    {

                        Console.WriteLine(">>>" + method.Invoke(typeof(T), new object[] {}));

                    }

                    //Console.WriteLine("reflection# " + i + " > : " + method.Name + " : " + method.ReturnType );

                }

            }

        }

    }


    class Program
    {
        static void Main(string[] args)
        {
            
            Vardump Vd  = new Vardump(typeof(VoClass));
            Vd.PropertyGet();
           
        }
    }
1개의 좋아요

@니꼴라오 정중하게 부탁 드립니다!

소스코드 블록 사용 방법!


코드 삽입하실때 첫줄과 끝 줄에 숫자 1 왼쪽에 있는 ` 특수문자를 3번 (```) 첫 줄과 끝 줄에 입력합니다.

image

키는 여기 있어요.


숫자 1번 왼쪽에 ~물결과 함께 있는 키입니다.

image

이렇게 코드를 씌워주시면 됩니다. :sweat_smile:

실행 결과


간단하죠? 이렇게 코드 블록이 생깁니다.

public void test()
{
}

꼭 적용해보시길 바랍니다!

3개의 좋아요

Invoke 메서드의 첫번째 인자는 Type 객체가 아닌 Invoke를 통해 얻어오고 싶은 인스턴스를 넣으셔야 합니다.
작성해주신 코드 기준으로는 메인 함수에 있는 Vd가 Invoke의 첫번째 인자로 들어갔을 때 원하는 결과를 얻을 수 있습니다.

다른 방법으로는 this를 첫번째 인자로 넣어보세요.

1개의 좋아요