This project is read-only.

HasDefaultConstructor

Topics: PE reader, Source Model
Oct 29, 2011 at 10:22 PM

Helo again,

How would you give answer to the question if a type definition has a default constructor?   

  (--or is there a better naming for that thing that could be called by an activator or by new MyType() without args)

 

i tried the following two approaches:

        public static bool HasDefaultConstructor(this ITypeDefinition type, IName ctorName)
        {
            //return TypeHelper.GetMethod(type, ctorName) != Dummy.Method;
            
            //return type.GetMatchingMembers(member => member.Name == ctorName).Any();
        }
    }

both producing false for any type that i tested (a lot).

i cached the ".ctor" name object like that:

            FctorName = FHost.NameTable.GetNameFor(".ctor");

 

Thanks,

Sebastian

Oct 29, 2011 at 11:18 PM

TypeHelper.GetMethod(type, FHost.NameTable.Ctor) should do the trick.

Oct 30, 2011 at 12:15 AM
Edited Oct 30, 2011 at 1:35 AM

thank you for your fast reply and that handy shortcut.

 

however its application still doesn't work for me.

when looking at the following class via CCI i am NOT getting a dummymethod which tells me, that there is no constructor without arguments. But there is?!

 

    public class ChangeDetector<T> //where T : IEquatable
    {
        T LastInput;
        bool FirstFrame;

        public ChangeDetector()
        {
            FirstFrame = true;
            LastInput = default(T);
        }

        [Node]
        public bool IsChanged(T input)
        {
            var changed = (FirstFrame || !LastInput.Equals(input));

            FirstFrame = false;
            LastInput = input;

            return changed;
        }
    }

 

thanks for help!

Sebastian

Oct 30, 2011 at 12:37 AM

I'm a bit confused by your question. If you are getting a method is which is not a dummy then that method ought to be the constructor you are looking for. Can you clarify?

Oct 30, 2011 at 1:08 AM

oh sorry. i AM getting a dummy method, so there is NO constructor without arguments. sorry.

 

so but the problem persits. i get no default constructor for the class above...

Oct 30, 2011 at 1:16 AM

Most likely your type definition is also a dummy.

One way to learn the API is to start with a sample and debug through it. It can also be very helpful to make your question concrete in the form of a small test program that displays the behavior that you are confused about. If the behavior is actually wrong (not quite so likely, but possible) then you have a handy bug report. If not, you have a concrete question that admits a precise answer together with a specific explanation.

Oct 30, 2011 at 1:34 AM
Edited Oct 30, 2011 at 1:39 AM

Visual Studio debugger says type is "{VVVV.Nodes.ChangeDetector}" which is the generic type definition above.

            var m = TypeHelper.GetMethod(type, ctorName); 
            return m != Dummy.Method;

for m i get "{Microsoft.Cci.DummyMethodDefinition}"

 

could the genericness be the problem? i also never defined a concrete type that uses a type parameter for type argument T.

i will try to make a small app and i will make sure that i use the latest sources. where should i upload it when i'm done?

Oct 30, 2011 at 1:37 AM

You can just attach it to this discussion, or if you are convinced it is a bug, you can open an issue and attach it to there.

Nov 5, 2011 at 4:05 AM

solved. i accidentaly created my host twice (sorry!!)