예제 #1
0
        public object InternalCreateInstance(Type type, object[] args)
        {
            if (type != null)
            {
                if (type.IsAbstract && type.IsSealed)
                {
                    return(type);
                }

#if !SILVERLIGHT
                if (_reflectionEmitPermission)
                {
                    ConstructorInvoker invoker;
                    _typeConstructorCache.TryGetValue(type, out invoker);
                    if (invoker == null)
                    {
                        invoker = ConstructorExtensions.DelegateForCreateInstance(type, args);
                        _typeConstructorCache[type] = invoker;
                    }
                    return(invoker(args));
                }
                return(Activator.CreateInstance(type, BindingFlags.CreateInstance | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static, null, args, null));
#else
                return(type.InvokeMember(null, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance | BindingFlags.Static, null, null, args));
#endif
            }
            return(null);
        }
        public void DelegateForCreateInstance_Invoke()
        {
            var createDelegator = ConstructorExtensions.DelegateForCreateInstance(widgetType, typeof(int), typeof(string));

            foreach (var widget in _widgets)
            {
                var dynamicWidget = createDelegator.Invoke(widget.Id, widget.Name);
                Assert.AreEqual(widget.GetArea(), dynamicWidget.CallMethod("GetArea"));
            }
        }