Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

249
Vistas
How to know if a variable is only a "pointer" to another object or if it can exist independently
$App = New-Object -TypeName Microsoft.SqlServer.Dts.Runtime.Application; 

$PackageFullPath = 'C:\SSISPackage.dtsx'; 

$Package = $App.LoadPackage($PackageFullPath, $null, 0);

$x = $Package.Connections
$x.Remove("Something")

$App.SaveToXml($PackageFullPath, $Package, $null)

My question is when I assign the instance to a new variable $x, how do I know when I call a method on $x that it will reflect on $Package? So when I remove someting from $x, will it also remove that thing from $Package? If the answer is "it depends" how can I know without setting up an experiment?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

tl;dr

Because the $Package.Connections property contains an instance of a .NET reference type, namely Microsoft.SqlServer.Dts.Runtime.Connections, $x and $Package.Connections reference the very same collection instance, so $x.Remove("Something") is the same as $Package.Connections.Remove("Something")


The behavior depends on whether a given value is an instance of a .NET reference type or value type:

  • When reference-type instances are assigned to a (new) variable / passed as an argument, it is the reference ("pointer") to the actual data that is copied, which means that both the original value and the target variable / parameter refer to the same object.

  • By contrast, assigning / passing a value-type instance makes a copy of the value itself, resulting in independent copies of the data.

You can examine an object stored in a given variable $x as follows: $true indicates a value-type instance, $false a reference-type instance:

$x.GetType().IsValueType

Note that collection-like types, including arrays, are reference-type instances, so $x and $Package.Connections in the code in your question will refer to the same collection.
By contrast, all so-called primitive types, such as numbers, are value types.

If you don't want to rely on inspection at runtime, you can examine the documentation for a given type, which (with the language set to C#) will use the following "type-kind" identifiers:

  • Reference types:
    • class (e.g. ArrayList)
  • Value types:
    • struct (e.g., DateTime)
    • enum (e.g., PlatformID)
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda