如何在“类型参数的约束”中使用通过运行时反射提取的类型?

发布于 2024-11-01 20:12:23 字数 4431 浏览 0 评论 0原文

请仔细阅读示例代码:

    public T LoadWithChildren<T>(object key, object myObject) where T : class, new()
    {
        //T myObject = new T();
        SelectOne(myObject, key);

        /// Caching all non-generic type objects.
        Type objectType = typeof(T);

        PropertyInfo[] propertyInfo = objectType.GetProperties();
        List<object> NonGenericProperty = new List<object>();
        List<object> Parents = new List<object>();
        List<object> Children = new List<object>();

        foreach (PropertyInfo property in propertyInfo)
        {
            if (property.PropertyType.IsPrimitive == false && property.PropertyType.FullName != "System.Byte"
                && property.PropertyType.Name != "System.SByte" && property.PropertyType.Name != "System.Int32"
                && property.PropertyType.Name != "System.UInt32" && property.PropertyType.Name != "System.Int16"
                && property.PropertyType.Name != "System.UInt16" && property.PropertyType.Name != "System.Int64"
                && property.PropertyType.Name != "System.UInt64" && property.PropertyType.Name != "System.Single"
                && property.PropertyType.Name != "System.Double" && property.PropertyType.Name != "System.Char"
                && property.PropertyType.Name != "System.Boolean" && property.PropertyType.Name != "System.Object"
                && property.PropertyType.Name != "System.Object" && property.PropertyType.Name != "System.String"
                && property.PropertyType.Name != "System.Decimal")
            {
                NonGenericProperty.Add(property);
            }
        }

        /// Separate All children 
        foreach (object NonGenericObject in NonGenericProperty)
        {
            /// Checking for child attributes
            foreach (object attribute in ((PropertyInfo)NonGenericObject).GetCustomAttributes(true))
            {
                /// If the attribute is ChildObjectAttribute
                if (attribute is ChildObjectAttribute)
                {
                    Children.Add(NonGenericObject);

                    Type tempType = ((PropertyInfo)NonGenericObject).PropertyType;

                    PropertyInfo[] tempProperty = tempType.GetProperties();


                    // if the child is not a Generic type like list
                    if (((PropertyInfo)NonGenericObject).PropertyType.IsGenericType == false)
                    {
                        foreach (PropertyInfo property in tempProperty)
                        {
                            foreach (object childAttribute in property.GetCustomAttributes(true))
                            {
                                if (childAttribute is ParentObjectAttribute && property.PropertyType == typeof(T))
                                {
                                    /// Set the parent reference of child object to myObject
                                    object childObject = new object();
                                    property.SetValue(childObject, myObject, null);
                                    LoadWithChildren<tempType>(1, childObject);


                                }
                            }
                        }
                    }
                    /*if (((PropertyInfo)NonGenericObject).PropertyType.IsGenericType == true)
                    {
                        foreach(PropertyInfo property )
                        {

                        }
                    }*/
                }
            }
        }

        /// Separate All parents  
        foreach (object NonGenericObject in NonGenericProperty)
        {
            foreach (object attribute in ((PropertyInfo)NonGenericObject).GetCustomAttributes(true))
            {
                if (attribute is ParentObjectAttribute)
                {
                    Parents.Add(NonGenericObject);
                }
            }
        }


        /// Caching all  






        throw new NotImplementedException();
    }

如果您看到最后当方法 LoadWithChildren(object key, object myObject) 递归调用自身时,我尝试使用“tempType”,一个“Type”对象作为类型参数,因为类型未知,并且正在使用反射来获取运行时的类型。但它不接受“Type”对象,而是要求一个类或无参数构造函数。如果有人能帮助我,我将不胜感激。您的建议、解决方案和知识将不胜感激。

问候, 乌迈尔

Please go through the sample code:

    public T LoadWithChildren<T>(object key, object myObject) where T : class, new()
    {
        //T myObject = new T();
        SelectOne(myObject, key);

        /// Caching all non-generic type objects.
        Type objectType = typeof(T);

        PropertyInfo[] propertyInfo = objectType.GetProperties();
        List<object> NonGenericProperty = new List<object>();
        List<object> Parents = new List<object>();
        List<object> Children = new List<object>();

        foreach (PropertyInfo property in propertyInfo)
        {
            if (property.PropertyType.IsPrimitive == false && property.PropertyType.FullName != "System.Byte"
                && property.PropertyType.Name != "System.SByte" && property.PropertyType.Name != "System.Int32"
                && property.PropertyType.Name != "System.UInt32" && property.PropertyType.Name != "System.Int16"
                && property.PropertyType.Name != "System.UInt16" && property.PropertyType.Name != "System.Int64"
                && property.PropertyType.Name != "System.UInt64" && property.PropertyType.Name != "System.Single"
                && property.PropertyType.Name != "System.Double" && property.PropertyType.Name != "System.Char"
                && property.PropertyType.Name != "System.Boolean" && property.PropertyType.Name != "System.Object"
                && property.PropertyType.Name != "System.Object" && property.PropertyType.Name != "System.String"
                && property.PropertyType.Name != "System.Decimal")
            {
                NonGenericProperty.Add(property);
            }
        }

        /// Separate All children 
        foreach (object NonGenericObject in NonGenericProperty)
        {
            /// Checking for child attributes
            foreach (object attribute in ((PropertyInfo)NonGenericObject).GetCustomAttributes(true))
            {
                /// If the attribute is ChildObjectAttribute
                if (attribute is ChildObjectAttribute)
                {
                    Children.Add(NonGenericObject);

                    Type tempType = ((PropertyInfo)NonGenericObject).PropertyType;

                    PropertyInfo[] tempProperty = tempType.GetProperties();


                    // if the child is not a Generic type like list
                    if (((PropertyInfo)NonGenericObject).PropertyType.IsGenericType == false)
                    {
                        foreach (PropertyInfo property in tempProperty)
                        {
                            foreach (object childAttribute in property.GetCustomAttributes(true))
                            {
                                if (childAttribute is ParentObjectAttribute && property.PropertyType == typeof(T))
                                {
                                    /// Set the parent reference of child object to myObject
                                    object childObject = new object();
                                    property.SetValue(childObject, myObject, null);
                                    LoadWithChildren<tempType>(1, childObject);


                                }
                            }
                        }
                    }
                    /*if (((PropertyInfo)NonGenericObject).PropertyType.IsGenericType == true)
                    {
                        foreach(PropertyInfo property )
                        {

                        }
                    }*/
                }
            }
        }

        /// Separate All parents  
        foreach (object NonGenericObject in NonGenericProperty)
        {
            foreach (object attribute in ((PropertyInfo)NonGenericObject).GetCustomAttributes(true))
            {
                if (attribute is ParentObjectAttribute)
                {
                    Parents.Add(NonGenericObject);
                }
            }
        }


        /// Caching all  






        throw new NotImplementedException();
    }

if you see that at the end when the method LoadWithChildren(object key, object myObject) is recursively calling itself, I am trying to use "tempType", a "Type" object as Type Parameter since the type is not known and reflection is being used to get the type on run time. But its not accepting the "Type" object rather it asks for a class or a parameter less constructor. I would be obliged if someone can help me out. You suggestions, solutions and knowledge would be appreciated.

Regards,
Umair

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花细雨 2024-11-08 20:12:23

您将必须使用 MakeGenericMethod() 因为您在编译时不知道要使用什么类型:

MethodInfo method = typeof(WhateverContainsThisMethod).GetMethod("LoadWithChildren");
MethodInfo mi= method.MakeGenericMethod(tempType);
mi.Invoke(this, new object[] {1, childObject} );

You are going to have to use MakeGenericMethod() since you don't know at compile time what type you are going to use:

MethodInfo method = typeof(WhateverContainsThisMethod).GetMethod("LoadWithChildren");
MethodInfo mi= method.MakeGenericMethod(tempType);
mi.Invoke(this, new object[] {1, childObject} );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文