Typed factories in C#3 – V2

This is a follow up on my previous post: http://rogeralsing.com/2008/02/21/typed-factories-in-c3-managed-new/

 I’ve added support for property and field initializers.
This makes it possible to call your factory like this:

Person person = MyFactory.Create(() => new Person("Roger", "Alsing") { 
    Age = 32, 
    SomeIntField = 4, 
    SomeIntArray = new int[]{1,2,3} 
});

You can find the code for the improved factory class here:
templatedfactorycs.txt   (WordPress don’t like .cs files)

Enjoy..

10 Comments

  1. Tony Caduto says:

    Roger,
    I have been trying to post some messages at the puzzle framework webpage but get error after error also the old compona site is down with a .net 1.1 error.

    I fixed a couple of issues in the SyntaxBox control and would like to send you the fixes.
    One is a real easy one for the OverWrite property, (it currently does not work) and for the code completion (autolist)

    Thanks,

    Tony Caduto

  2. Tony Caduto says:

    Here are the fixes I did.
    I am new to C# so the autolist one is probably not the best way to fix it but it works better than before:

    Bug fixes for Syntax Box

    First I ran into the OverWrite property was not working.

    The fix for this was to modify the OverWrite property in SyntaxBoxControl.cs

    public bool OverWrite
    {
    get
    {
    EditViewControl aview = (EditViewControl)_ActiveView;
    return aview.OverWrite;
    }
    }

    The property was not taking into account that the control has multiple view capability.

    The next problem I ran into was the code completion autolist was not working properly.
    Basicly it was not using the first few words of typing to select the closest match in the autolist box.

    I had to make a few mods to get this working the way I was expecting it to work first:

    protected override void OnKeyPress(KeyPressEventArgs e)

    which is in EditViewControl.cs. In the OnKeyPress method there the following if statement:
    if (AutoListVisible && !e.Handled && _SyntaxBox.AutoListAutoSelect)

    Which I changed to:

    if (AutoListVisible && !e.Handled && _SyntaxBox.AutoListAutoSelect)
    {
    try
    {
    string s = “”;
    if (Caret.CurrentWord != null)
    s = Caret.CurrentWord.Text;
    AutoList.SelectItem(s);
    }
    catch
    {
    }
    }

    It was using the X coordinate where the user initiated the autolist, and this of course was always blank, unless the user typed
    after the list was displayed and it would never grab whatever they typed before starting the autolist.

    The next fix involves the InsertAutolistText method in EditViewControl.cs. Again this method was not taking into account if
    the user had typed a few chars and was inserting the selection after instead of replacing it. Changed to:

    public void InsertAutolistText()
    {
    int startpos = 0;
    startpos = AutoListStartPos.X;
    //is there a current word?
    if (Caret.CurrentWord != null)
    {
    int ccol = Caret.CurrentWord.Column;
    if (ccol != startpos && Caret.CurrentWord.Text != ” “)
    {
    int cwlength = 0;
    cwlength = Caret.CurrentWord.Text.Length;
    startpos = startpos – cwlength;
    }
    }

    TextRange tr = new TextRange();
    tr.FirstRow = Caret.Position.Y;
    tr.LastRow = Caret.Position.Y;
    tr.FirstColumn = startpos;
    tr.LastColumn = Caret.Position.X;
    Document.DeleteRange(tr, true);
    Caret.Position.X = startpos;
    this.InsertText(AutoList.SelectedText);
    SetFocus();
    }

  3. Roger Alsing says:

    Hi Tony thanks for the fixes, Ill apply them asap :-)

  4. Tony Caduto says:

    Roger,
    I would post at the puzzel website forum, but it won’t let me register.

    Have you considered hosting the SyntaxBox control on CodePlex?
    I bet it woud get a lot more contributers. I was looking for days before I found SyntaxBox and I really like it.

    I see the Fireball thing is on there(codeplex), but I don’t think they did many fixes/updates if any to the control. plus they have it as part of some hacked together framework of other opensource controls.

    The other thing I am going to look at doing is allowing the Autolist box to expose it’s datasource property so it can be populated from another list or datatable.

    Thanks,

    Tony

  5. Roger Alsing says:

    Oh I didnt know about the registration problems on the site :-/
    Will fix that asap.

    But yes, we are considering publishing all of the puzzle stuff on CodePlex.

    Ill get back to you on that one once Ive got my real computer working again.
    (only got vs.net2008 on this game machine)

  6. Tony Caduto says:

    Hi Roger,

    The old Compona site is down as well. That’s the only place to get the documentation.

    Thanks,

    Tony

  7. Tony says:

    Roger,
    The registration works, but the posting of messages does not work.

    It errors out saying I must register, even though I am logged in.

    Also the contact form does not work.

    Thanks,

    Tony

  8. Tony Caduto says:

    Sorry for posting here, but I still can’t post messages on the forum.
    Found another real easy bug fix:

    In EditViewControl.cs the CreateAutoList method needs the following lines removed or commented out:

    this.AutoList.Add(“a123”, “a123”, “Some tooltip for this item 1”, 1);
    this.AutoList.Add(“b456”, “b456”, “Some tooltip for this item 2”, 2);
    this.AutoList.Add(“c789”, “c789”, “Some tooltip for this item 3”, 2);
    this.AutoList.Add(“d012”, “d012”, “Some tooltip for this item 4”, 3);
    this.AutoList.Add(“e345”, “e345”, “Some tooltip for this item 5”, 4);

    It’s causing “Some tooltip for this item” to show even if no tooltip is set.

  9. Tony Caduto says:

    Sorry again, but the first fix I posted for InsertAutoList text did not work in all situations, this one does(so far).

    public void InsertAutolistText()
    {
    int caretx = 0;
    TextRange tr = new TextRange();
    tr.FirstRow = Caret.Position.Y;
    tr.LastRow = Caret.Position.Y;
    if (Caret.CurrentWord != null && Caret.CurrentWord.Type == WordType.xtWord)
    {
    tr.FirstColumn = Caret.CurrentWord.Column;
    tr.LastColumn = Caret.CurrentWord.Column + Caret.CurrentWord.Text.Length;
    caretx = Caret.CurrentWord.Column + AutoList.SelectedText.Length;
    }
    else
    {
    tr.FirstColumn = AutoListStartPos.X;
    tr.LastColumn = Caret.Position.X;
    caretx = tr.FirstColumn + AutoList.SelectedText.Length+1;
    }
    Document.DeleteRange(tr, true);
    Caret.Position.X = caretx;
    this.InsertText(AutoList.SelectedText);
    SetFocus();
    }

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s