Instantiating from Type T

Posts   
 
    
Posts: 69
Joined: 24-Jun-2008
# Posted on: 31-Jul-2008 11:55:01   

Hi,

I am developing a base class for editing entities in ASP.NET. The base class is able to manage the loading and saving of the entity.

The only thing I have troubles with is how to load an object if in the template class. For example:

// Create or get object
T entity = (_objectID <= 0) ? new ((EntityBase)T) : new ProjectEntity(_objectID);

Do you have any idea how I can:

1) Create a new instance of T 2) Load an existing instance of T with dynamic constructor loading (assume I know all the primary key fields of the object)

Best regards,

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 31-Jul-2008 16:21:16   

I don't quite understand the question. Would you please explain again in more details and post a complete code snippet?

Posts: 69
Joined: 24-Jun-2008
# Posted on: 31-Jul-2008 16:30:53   

The problem is I have a base class where I don't have a strong name of the type yet. But, in the base class, I want to be able to retrieve the entity.

I have fixed the problem using reflection:

        /// <summary>
        /// Gets the currently loaded object represented by this page
        /// </summary>
        protected T CurrentObject
        {
            get
            {
                // Create or get object
                object entity = null;
                if (_pageMode == ObjectPageBase<T>.ObjectPageMode.Edit)
                {
                    // Load existing, get constructor info
                    List<Type> constructorArgumentTypes = new List<Type>();
                    List<object> constructorArguments = new List<object>();
                    foreach (ObjectIdentifier identifier in _identifiers)
                    {
                        constructorArgumentTypes.Add(identifier.Type);
                        constructorArguments.Add(identifier.Value);
                    }

                    // Get constructor info
                    ConstructorInfo constructorInfo = typeof(T).GetConstructor(constructorArgumentTypes.ToArray());

                    // Check if there is a valid constructor
                    if (constructorInfo == null)
                    {
                        // Throw exception
                        throw new ObjectSaveException();
                    }

                    // Invoke constructor
                    entity = (T)constructorInfo.Invoke(constructorArguments.ToArray());
                }
                else
                {
                    // Create new, get constructor info
                    ConstructorInfo constructorInfo = typeof(T).GetConstructor(new Type[] { });
                    if (constructorInfo == null)
                    {
                        // Throw exception
                        throw new ObjectSaveException();
                    }

                    // Invoke constructor
                    entity = (T)constructorInfo.Invoke(null);
                }

                // Return object
                return (T)entity;
            }
        }

The ObjectIndentifier object is a simple class which contains the identifier, type of the identifier and the value if one. This way, I can runtime load / save objects without knowing the type. This saves me a LOT of time when I want to create an edit page for a specific object. I can now simple derive from the base class which handles the loading / saving of the object and I only need to worry about the object representation itself.

If there is a better way, please let me know!

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 31-Jul-2008 16:52:34   

I think this thread might be helpful here: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=8445

Posts: 69
Joined: 24-Jun-2008
# Posted on: 31-Jul-2008 19:31:01   

The method described by Matthew only supports 1 PK (and it must be an int).

It seems that the reflection (calling the constructors and use those with the right type) is a good method.