This project is read-only.

Is there any ways to add attributes to any ContentItem inherited class at runtime?

Topics: Developer Forum
Feb 4, 2009 at 7:18 PM
Is there any ways to add attributes to any ContentItem inherited class at runtime?
(It's for adding field at admin backend when editing ContentItem element.)

I'm trying to do some samples, but i can't get any results..

namespace TemplateWorks.Services
{
    /// <summary>
    /// Registers components and services needed by the TemplateWorks project.
    /// </summary>
    [AutoInitialize]
    public class Manager : IPluginInitializer
    {
        public void Initialize(IEngine engine)
        {
            Attribute[] attrs = new Attribute[1];
            WithEditableAttribute atr =
                new WithEditableAttribute("Layout", typeof(N2.Details.EditableTextBoxAttribute), "Layout", 7, "Layout")
                {
                    ContainerName = Tabs.Content
                };
            attrs[0] = atr;
            TypeDescriptor.AddAttributes(typeof(N2.Templates.Items.AbstractContentPage), attrs);
            
        }
    }
}
Feb 4, 2009 at 9:53 PM
I just want to add some fileds at runtime, without editing of sources.
Feb 4, 2009 at 10:25 PM
N2 doens't use TypeDescriptor to retrieve attributes. What you can do is use this technique to modify the ItemDefinition with external editables.
Feb 5, 2009 at 7:41 AM
Edited Feb 5, 2009 at 8:57 AM
What could you advice me.
I want create module wich will add some fields to ContentItems programmatically.
Maybe N2 has built-in methods for autocreating edit fields.

P.S.: Technique you pointed require to make changes in the sources. But maybe a haven't full understanding.
Feb 5, 2009 at 8:58 AM
Thank you libardo.
This is what i have been searching for.
Feb 5, 2009 at 9:50 AM
Aha! I suspected there's something clever instead of TypeDescriptor. I wonder, will it allow to inject a [TemplateAttribute] to existing classes? If so, than it'll close a question: "how to override a design-time templates without touching the code"..

Btw, the idea to design an alternative rendering engine based ot TypeDescriptor/TypeEditor/TypeConverter came to mind some time ago. A prototype worked quite well in WinForm app with a universal PropertyGrid. I think something like that would be possible on the web -- even hope to show one such result of my obsession with ItemEditor soon...

Btw (2), one intermediate result of the said obsession with the ItemEditor was an idea to "improve" IEditable with it's

bool UpdateItem(ContentItem item, Control editor);
void UpdateEditor(ContentItem item, Control editor);

to some IExsitingItemEditor with the following inside:

bool UpdateItem(ContentItem item);
event EventHandler<ItemEditorEventArgs<ContentItem>> UpdateEditor;

Spot it? The main idea is to delegate an imperative UpdateEditor method call to the UpdateEditor event, so that an editor could decide on itself when to pull the CurrentItem. This way, editor control internals could be greatly simplified as there'll be no need to keep track of a CurrentItem -- it just could be requested when needed by firing an event (much the same way as the ObjectDataSource.ObjectCreating does it).

Sorry for being so talkative, should consider blogging at last :-)
Feb 5, 2009 at 9:53 PM
There is something in the trunk with the TemplateAttributes. They are basically stored stored in a global variable.

The type descriptor API is a blank spot on my .NET map. I'll check out what you've been doing. When I wrote the ItemDefinition stuff I was very much against static state and global variables.

I'm understanding the the IExistingItemEditor as a way to delegate work the the web control. What are the benefits? Sorry for not understanding =) And thanks for writing. I find these subjects very interesting.