1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
|
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
// Activator is an object that contains the Activation (CreateInstance/New)
// methods for late bound support.
//
//
//
//
namespace System {
using System;
using System.Reflection;
using System.Runtime.Remoting;
#if FEATURE_REMOTING
using System.Runtime.Remoting.Activation;
// using Message = System.Runtime.Remoting.Messaging.Message;
#endif
using System.Security;
using CultureInfo = System.Globalization.CultureInfo;
using Evidence = System.Security.Policy.Evidence;
using StackCrawlMark = System.Threading.StackCrawlMark;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using System.Security.Permissions;
using AssemblyHashAlgorithm = System.Configuration.Assemblies.AssemblyHashAlgorithm;
using System.Runtime.Versioning;
using System.Diagnostics.Contracts;
// Only statics, does not need to be marked with the serializable attribute
[ClassInterface(ClassInterfaceType.None)]
[ComDefaultInterface(typeof(_Activator))]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class Activator : _Activator
{
internal const int LookupMask = 0x000000FF;
internal const BindingFlags ConLookup = (BindingFlags) (BindingFlags.Instance | BindingFlags.Public);
internal const BindingFlags ConstructorDefault= BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance;
// This class only contains statics, so hide the worthless constructor
private Activator()
{
}
// CreateInstance
// The following methods will create a new instance of an Object
// Full Binding Support
// For all of these methods we need to get the underlying RuntimeType and
// call the Impl version.
static public Object CreateInstance(Type type,
BindingFlags bindingAttr,
Binder binder,
Object[] args,
CultureInfo culture)
{
return CreateInstance(type, bindingAttr, binder, args, culture, null);
}
[System.Security.SecuritySafeCritical] // auto-generated
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
static public Object CreateInstance(Type type,
BindingFlags bindingAttr,
Binder binder,
Object[] args,
CultureInfo culture,
Object[] activationAttributes)
{
if ((object)type == null)
throw new ArgumentNullException("type");
Contract.EndContractBlock();
#if !FULL_AOT_RUNTIME
if (RuntimeFeature.IsDynamicCodeSupported && type is System.Reflection.Emit.TypeBuilder)
throw new NotSupportedException(Environment.GetResourceString("NotSupported_CreateInstanceWithTypeBuilder"));
#endif
// If they didn't specify a lookup, then we will provide the default lookup.
if ((bindingAttr & (BindingFlags) LookupMask) == 0)
bindingAttr |= Activator.ConstructorDefault;
if (activationAttributes != null && activationAttributes.Length > 0){
// If type does not derive from MBR
// throw notsupportedexception
#if FEATURE_REMOTING
if(type.IsMarshalByRef){
// The fix below is preventative.
//
if(!(type.IsContextful)){
if(activationAttributes.Length > 1 || !(activationAttributes[0] is UrlAttribute))
throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonUrlAttrOnMBR"));
}
}
else
#endif
throw new NotSupportedException(Environment.GetResourceString("NotSupported_ActivAttrOnNonMBR" ));
}
RuntimeType rt = type.UnderlyingSystemType as RuntimeType;
if (rt == null)
throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"),"type");
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return rt.CreateInstanceImpl(bindingAttr,binder,args,culture,activationAttributes, ref stackMark);
}
static public Object CreateInstance(Type type, params Object[] args)
{
return CreateInstance(type,
Activator.ConstructorDefault,
null,
args,
null,
null);
}
static public Object CreateInstance(Type type,
Object[] args,
Object[] activationAttributes)
{
return CreateInstance(type,
Activator.ConstructorDefault,
null,
args,
null,
activationAttributes);
}
static public Object CreateInstance(Type type)
{
return Activator.CreateInstance(type, false);
}
/*
* Create an instance using the name of type and the assembly where it exists. This allows
* types to be created remotely without having to load the type locally.
*/
[System.Security.SecuritySafeCritical] // auto-generated
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
static public ObjectHandle CreateInstance(String assemblyName,
String typeName)
{
#if MONO
if(assemblyName == null)
assemblyName = Assembly.GetCallingAssembly ().GetName ().Name;
#endif
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return CreateInstance(assemblyName,
typeName,
false,
Activator.ConstructorDefault,
null,
null,
null,
null,
null,
ref stackMark);
}
[System.Security.SecuritySafeCritical] // auto-generated
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
static public ObjectHandle CreateInstance(String assemblyName,
String typeName,
Object[] activationAttributes)
{
#if MONO
if(assemblyName == null)
assemblyName = Assembly.GetCallingAssembly ().GetName ().Name;
#endif
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return CreateInstance(assemblyName,
typeName,
false,
Activator.ConstructorDefault,
null,
null,
null,
activationAttributes,
null,
ref stackMark);
}
public static object CreateInstance(Type type, bool nonPublic)
{
return CreateInstance(type, nonPublic, wrapExceptions: true);
}
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
internal static object CreateInstance(Type type, bool nonPublic, bool wrapExceptions)
{
if ((object)type == null)
throw new ArgumentNullException("type");
Contract.EndContractBlock();
RuntimeType rt = type.UnderlyingSystemType as RuntimeType;
if (rt == null)
throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "type");
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return rt.CreateInstanceDefaultCtor(!nonPublic, false, true, wrapExceptions, ref stackMark);
}
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
static public T CreateInstance<T>()
{
RuntimeType rt = typeof(T) as RuntimeType;
// This is a hack to maintain compatibility with V2. Without this we would throw a NotSupportedException for void[].
// Array, Ref, and Pointer types don't have default constructors.
if (rt.HasElementType)
throw new MissingMethodException(Environment.GetResourceString("Arg_NoDefCTor"));
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
// Skip the CreateInstanceCheckThis call to avoid perf cost and to maintain compatibility with V2 (throwing the same exceptions).
#if FEATURE_CORECLR
// In SL2/3 CreateInstance<T> doesn't do any security checks. This would mean that Assembly B can create instances of an internal
// type in Assembly A upon A's request:
// TypeInAssemblyA.DoWork() { AssemblyB.Create<InternalTypeInAssemblyA>();}
// TypeInAssemblyB.Create<T>() {return new T();}
// This violates type safety but we saw multiple user apps that have put a dependency on it. So for compatability we allow this if
// the SL app was built against SL2/3.
// Note that in SL2/3 it is possible for app code to instantiate public transparent types with public critical default constructors.
// Fortunately we don't have such types in out platform assemblies.
if (CompatibilitySwitches.IsAppEarlierThanSilverlight4 ||
CompatibilitySwitches.IsAppEarlierThanWindowsPhone8)
return (T)rt.CreateInstanceSlow(true /*publicOnly*/, true /*skipCheckThis*/, false /*fillCache*/, ref stackMark);
else
#endif // FEATURE_CORECLR
return (T)rt.CreateInstanceDefaultCtor(true /*publicOnly*/, true /*skipCheckThis*/, true /*fillCache*/, true /*wrapExceptions*/, ref stackMark);
}
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
static public ObjectHandle CreateInstanceFrom(String assemblyFile,
String typeName)
{
return CreateInstanceFrom(assemblyFile, typeName, null);
}
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
static public ObjectHandle CreateInstanceFrom(String assemblyFile,
String typeName,
Object[] activationAttributes)
{
return CreateInstanceFrom(assemblyFile,
typeName,
false,
Activator.ConstructorDefault,
null,
null,
null,
activationAttributes);
}
[System.Security.SecuritySafeCritical] // auto-generated
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
[Obsolete("Methods which use evidence to sandbox are obsolete and will be removed in a future release of the .NET Framework. Please use an overload of CreateInstance which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
static public ObjectHandle CreateInstance(String assemblyName,
String typeName,
bool ignoreCase,
BindingFlags bindingAttr,
Binder binder,
Object[] args,
CultureInfo culture,
Object[] activationAttributes,
Evidence securityInfo)
{
#if MONO
if(assemblyName == null)
assemblyName = Assembly.GetCallingAssembly ().GetName ().Name;
#endif
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return CreateInstance(assemblyName,
typeName,
ignoreCase,
bindingAttr,
binder,
args,
culture,
activationAttributes,
securityInfo,
ref stackMark);
}
[SecuritySafeCritical]
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
public static ObjectHandle CreateInstance(string assemblyName,
string typeName,
bool ignoreCase,
BindingFlags bindingAttr,
Binder binder,
object[] args,
CultureInfo culture,
object[] activationAttributes)
{
#if MONO
if(assemblyName == null)
assemblyName = Assembly.GetCallingAssembly ().GetName ().Name;
#endif
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return CreateInstance(assemblyName,
typeName,
ignoreCase,
bindingAttr,
binder,
args,
culture,
activationAttributes,
null,
ref stackMark);
}
[System.Security.SecurityCritical] // auto-generated
static internal ObjectHandle CreateInstance(String assemblyString,
String typeName,
bool ignoreCase,
BindingFlags bindingAttr,
Binder binder,
Object[] args,
CultureInfo culture,
Object[] activationAttributes,
Evidence securityInfo,
ref StackCrawlMark stackMark)
{
#if FEATURE_CAS_POLICY
if (securityInfo != null && !AppDomain.CurrentDomain.IsLegacyCasPolicyEnabled)
{
throw new NotSupportedException(Environment.GetResourceString("NotSupported_RequiresCasPolicyImplicit"));
}
#endif // FEATURE_CAS_POLICY
Type type = null;
Assembly assembly = null;
if (assemblyString == null) {
assembly = RuntimeAssembly.GetExecutingAssembly(ref stackMark);
} else {
RuntimeAssembly assemblyFromResolveEvent;
AssemblyName assemblyName = RuntimeAssembly.CreateAssemblyName(assemblyString, false /*forIntrospection*/, out assemblyFromResolveEvent);
if (assemblyFromResolveEvent != null) {
// Assembly was resolved via AssemblyResolve event
assembly = assemblyFromResolveEvent;
} else if (assemblyName.ContentType == AssemblyContentType.WindowsRuntime) {
// WinRT type - we have to use Type.GetType
type = Type.GetType(typeName + ", " + assemblyString, true /*throwOnError*/, ignoreCase);
} else {
// Classic managed type
assembly = RuntimeAssembly.InternalLoadAssemblyName(
assemblyName, securityInfo, null, ref stackMark,
true /*thrownOnFileNotFound*/, false /*forIntrospection*/, false /*suppressSecurityChecks*/);
}
}
if (type == null) {
// It's classic managed type (not WinRT type)
Log(assembly != null, "CreateInstance:: ", "Loaded " + assembly.FullName, "Failed to Load: " + assemblyString);
if(assembly == null) return null;
type = assembly.GetType(typeName, true /*throwOnError*/, ignoreCase);
}
Object o = Activator.CreateInstance(type,
bindingAttr,
binder,
args,
culture,
activationAttributes);
Log(o != null, "CreateInstance:: ", "Created Instance of class " + typeName, "Failed to create instance of class " + typeName);
if(o == null)
return null;
else {
ObjectHandle Handle = new ObjectHandle(o);
return Handle;
}
}
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
[Obsolete("Methods which use evidence to sandbox are obsolete and will be removed in a future release of the .NET Framework. Please use an overload of CreateInstanceFrom which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
static public ObjectHandle CreateInstanceFrom(String assemblyFile,
String typeName,
bool ignoreCase,
BindingFlags bindingAttr,
Binder binder,
Object[] args,
CultureInfo culture,
Object[] activationAttributes,
Evidence securityInfo)
{
#if FEATURE_CAS_POLICY
if (securityInfo != null && !AppDomain.CurrentDomain.IsLegacyCasPolicyEnabled)
{
throw new NotSupportedException(Environment.GetResourceString("NotSupported_RequiresCasPolicyImplicit"));
}
#endif // FEATURE_CAS_POLICY
return CreateInstanceFromInternal(assemblyFile,
typeName,
ignoreCase,
bindingAttr,
binder,
args,
culture,
activationAttributes,
securityInfo);
}
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
public static ObjectHandle CreateInstanceFrom(string assemblyFile,
string typeName,
bool ignoreCase,
BindingFlags bindingAttr,
Binder binder,
object[] args,
CultureInfo culture,
object[] activationAttributes)
{
return CreateInstanceFromInternal(assemblyFile,
typeName,
ignoreCase,
bindingAttr,
binder,
args,
culture,
activationAttributes,
null);
}
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
private static ObjectHandle CreateInstanceFromInternal(String assemblyFile,
String typeName,
bool ignoreCase,
BindingFlags bindingAttr,
Binder binder,
Object[] args,
CultureInfo culture,
Object[] activationAttributes,
Evidence securityInfo)
{
#if FEATURE_CAS_POLICY
Contract.Assert(AppDomain.CurrentDomain.IsLegacyCasPolicyEnabled || securityInfo == null);
#endif // FEATURE_CAS_POLICY
#pragma warning disable 618
Assembly assembly = Assembly.LoadFrom(assemblyFile, securityInfo);
#pragma warning restore 618
Type t = assembly.GetType(typeName, true, ignoreCase);
Object o = Activator.CreateInstance(t,
bindingAttr,
binder,
args,
culture,
activationAttributes);
Log(o != null, "CreateInstanceFrom:: ", "Created Instance of class " + typeName, "Failed to create instance of class " + typeName);
if(o == null)
return null;
else {
ObjectHandle Handle = new ObjectHandle(o);
return Handle;
}
}
//
// This API is designed to be used when a host needs to execute code in an AppDomain
// with restricted security permissions. In that case, we demand in the client domain
// and assert in the server domain because the server domain might not be trusted enough
// to pass the security checks when activating the type.
//
[System.Security.SecurityCritical] // auto-generated_required
public static ObjectHandle CreateInstance (AppDomain domain, string assemblyName, string typeName) {
if (domain == null)
throw new ArgumentNullException("domain");
Contract.EndContractBlock();
return domain.InternalCreateInstanceWithNoSecurity(assemblyName, typeName);
}
[System.Security.SecurityCritical] // auto-generated_required
[Obsolete("Methods which use evidence to sandbox are obsolete and will be removed in a future release of the .NET Framework. Please use an overload of CreateInstance which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
public static ObjectHandle CreateInstance (AppDomain domain,
string assemblyName,
string typeName,
bool ignoreCase,
BindingFlags bindingAttr,
Binder binder,
Object[] args,
CultureInfo culture,
Object[] activationAttributes,
Evidence securityAttributes) {
if (domain == null)
throw new ArgumentNullException("domain");
Contract.EndContractBlock();
#if FEATURE_CAS_POLICY
if (securityAttributes != null && !AppDomain.CurrentDomain.IsLegacyCasPolicyEnabled)
{
throw new NotSupportedException(Environment.GetResourceString("NotSupported_RequiresCasPolicyImplicit"));
}
#endif // FEATURE_CAS_POLICY
return domain.InternalCreateInstanceWithNoSecurity(assemblyName, typeName, ignoreCase, bindingAttr, binder, args, culture, activationAttributes, securityAttributes);
}
[SecurityCritical]
public static ObjectHandle CreateInstance(AppDomain domain,
string assemblyName,
string typeName,
bool ignoreCase,
BindingFlags bindingAttr,
Binder binder,
object[] args,
CultureInfo culture,
object[] activationAttributes)
{
if (domain == null)
throw new ArgumentNullException("domain");
Contract.EndContractBlock();
return domain.InternalCreateInstanceWithNoSecurity(assemblyName,
typeName,
ignoreCase,
bindingAttr,
binder,
args,
culture,
activationAttributes,
null);
}
//
// This API is designed to be used when a host needs to execute code in an AppDomain
// with restricted security permissions. In that case, we demand in the client domain
// and assert in the server domain because the server domain might not be trusted enough
// to pass the security checks when activating the type.
//
[System.Security.SecurityCritical] // auto-generated_required
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
public static ObjectHandle CreateInstanceFrom (AppDomain domain, string assemblyFile, string typeName) {
if (domain == null)
throw new ArgumentNullException("domain");
Contract.EndContractBlock();
return domain.InternalCreateInstanceFromWithNoSecurity(assemblyFile, typeName);
}
[System.Security.SecurityCritical] // auto-generated_required
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
[Obsolete("Methods which use Evidence to sandbox are obsolete and will be removed in a future release of the .NET Framework. Please use an overload of CreateInstanceFrom which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
public static ObjectHandle CreateInstanceFrom (AppDomain domain,
string assemblyFile,
string typeName,
bool ignoreCase,
BindingFlags bindingAttr,
Binder binder,
Object[] args,
CultureInfo culture,
Object[] activationAttributes,
Evidence securityAttributes) {
if (domain == null)
throw new ArgumentNullException("domain");
Contract.EndContractBlock();
#if FEATURE_CAS_POLICY
if (securityAttributes != null && !AppDomain.CurrentDomain.IsLegacyCasPolicyEnabled)
{
throw new NotSupportedException(Environment.GetResourceString("NotSupported_RequiresCasPolicyImplicit"));
}
#endif // FEATURE_CAS_POLICY
return domain.InternalCreateInstanceFromWithNoSecurity(assemblyFile, typeName, ignoreCase, bindingAttr, binder, args, culture, activationAttributes, securityAttributes);
}
[SecurityCritical]
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
public static ObjectHandle CreateInstanceFrom(AppDomain domain,
string assemblyFile,
string typeName,
bool ignoreCase,
BindingFlags bindingAttr,
Binder binder,
object[] args,
CultureInfo culture,
object[] activationAttributes)
{
if (domain == null)
throw new ArgumentNullException("domain");
Contract.EndContractBlock();
return domain.InternalCreateInstanceFromWithNoSecurity(assemblyFile,
typeName,
ignoreCase,
bindingAttr,
binder,
args,
culture,
activationAttributes,
null);
}
#if FEATURE_COMINTEROP || MONO_COM || MOBILE_LEGACY
#if FEATURE_CLICKONCE || MOBILE_LEGACY
#if FEATURE_CLICKONCE || MONO_FEATURE_MULTIPLE_APPDOMAINS
[System.Security.SecuritySafeCritical] // auto-generated
public static ObjectHandle CreateInstance (ActivationContext activationContext) {
AppDomainManager domainManager = AppDomain.CurrentDomain.DomainManager;
if (domainManager == null)
domainManager = new AppDomainManager();
return domainManager.ApplicationActivator.CreateInstance(activationContext);
}
[System.Security.SecuritySafeCritical] // auto-generated
public static ObjectHandle CreateInstance (ActivationContext activationContext, string[] activationCustomData) {
AppDomainManager domainManager = AppDomain.CurrentDomain.DomainManager;
if (domainManager == null)
domainManager = new AppDomainManager();
return domainManager.ApplicationActivator.CreateInstance(activationContext, activationCustomData);
}
#else
[Obsolete ("Activator.CreateInstance (ActivationContext) is not supported on this platform.", true)]
public static ObjectHandle CreateInstance (ActivationContext activationContext) {
throw new PlatformNotSupportedException ("Activator.CreateInstance (ActivationContext) is not supported on this platform.");
}
[Obsolete ("Activator.CreateInstance (ActivationContext, string[]) is not supported on this platform.", true)]
public static ObjectHandle CreateInstance (ActivationContext activationContext, string[] activationCustomData) {
throw new PlatformNotSupportedException ("Activator.CreateInstance (ActivationContext) is not supported on this platform.");
}
#endif
#endif // FEATURE_CLICKONCE
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
public static ObjectHandle CreateComInstanceFrom(String assemblyName,
String typeName)
{
return CreateComInstanceFrom(assemblyName,
typeName,
null,
AssemblyHashAlgorithm.None);
}
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
public static ObjectHandle CreateComInstanceFrom(String assemblyName,
String typeName,
byte[] hashValue,
AssemblyHashAlgorithm hashAlgorithm)
{
Assembly assembly = Assembly.LoadFrom(assemblyName, hashValue, hashAlgorithm);
Type t = assembly.GetType(typeName, true, false);
Object[] Attr = t.GetCustomAttributes(typeof(ComVisibleAttribute),false);
if (Attr.Length > 0)
{
if (((ComVisibleAttribute)Attr[0]).Value == false)
throw new TypeLoadException(Environment.GetResourceString( "Argument_TypeMustBeVisibleFromCom" ));
}
Log(assembly != null, "CreateInstance:: ", "Loaded " + assembly.FullName, "Failed to Load: " + assemblyName);
if(assembly == null) return null;
Object o = Activator.CreateInstance(t,
Activator.ConstructorDefault,
null,
null,
null,
null);
Log(o != null, "CreateInstance:: ", "Created Instance of class " + typeName, "Failed to create instance of class " + typeName);
if(o == null)
return null;
else {
ObjectHandle Handle = new ObjectHandle(o);
return Handle;
}
}
#endif // FEATURE_COMINTEROP
#if !DISABLE_REMOTING && (FEATURE_REMOTING || MOBILE_LEGACY)
// This method is a helper method and delegates to the remoting
// services to do the actual work.
[System.Security.SecurityCritical] // auto-generated_required
static public Object GetObject(Type type, String url)
{
return GetObject(type, url, null);
}
// This method is a helper method and delegates to the remoting
// services to do the actual work.
[System.Security.SecurityCritical] // auto-generated_required
static public Object GetObject(Type type, String url, Object state)
{
if (type == null)
throw new ArgumentNullException("type");
Contract.EndContractBlock();
return RemotingServices.Connect(type, url, state);
}
#endif
[System.Diagnostics.Conditional("_DEBUG")]
private static void Log(bool test, string title, string success, string failure)
{
#if FEATURE_REMOTING
if(test)
BCLDebug.Trace("REMOTE", "{0}{1}", title, success);
else
BCLDebug.Trace("REMOTE", "{0}{1}", title, failure);
#endif
}
#if !FEATURE_CORECLR
void _Activator.GetTypeInfoCount(out uint pcTInfo)
{
throw new NotImplementedException();
}
void _Activator.GetTypeInfo(uint iTInfo, uint lcid, IntPtr ppTInfo)
{
throw new NotImplementedException();
}
void _Activator.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
{
throw new NotImplementedException();
}
// If you implement this method, make sure to include _Activator.Invoke in VM\DangerousAPIs.h and
// include _Activator in SystemDomain::IsReflectionInvocationMethod in AppDomain.cpp.
void _Activator.Invoke(uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
{
throw new NotImplementedException();
}
#endif
}
}
|