Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save warrenbuckley/a6adc536924c8528331450b929aba877 to your computer and use it in GitHub Desktop.
Save warrenbuckley/a6adc536924c8528331450b929aba877 to your computer and use it in GitHub Desktop.
DisableOtherPackageComponent.cs
public class MyComposer : IUserComposer
{
public void Compose(Composition composition)
{
// Register service into the container
composition.RegisterUnique<PaletteService>();
composition.RegisterUnique<ThemeService>();
// Add our PVC to the collection of stuff
composition.PropertyValueConverters().Append<ThemePickerPropertyConvertor>();
composition.Components().Append<ColorUpdateComponent>();
var otherPackageComponentType = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(t => t.GetTypes())
.FirstOrDefault(x => x.FullName == "SomePackage.Components.TheirComponent");
if (composition.Components().Has(otherPackageComponentType))
{
composition.Components().Remove(otherPackageComponentType);
}
}
}
}
@zpqrtbnk
Copy link

Assuming otherPackageComponentType is added by otherPackageComposerType, mark your composer with
[ComposeAfter(otherPackageComposerType)]
and it will execute after that composer, letting you remove the component.

@zpqrtbnk
Copy link

OK, questions:

  • Do you have access to otherPackageComposerType?
  • Do you have access to otherPackageComponentType?

@warrenbuckley
Copy link
Author

I can't do the following though @zpqrtbnk as my package does not directly reference the other package I am trying to disable so I don't have their type in my project so cant do [ComposeAfter(typeof(SomePackage.Components.TheirComponent))]

@zpqrtbnk
Copy link

zpqrtbnk commented Mar 25, 2020

Would it be OK to entirely disable the otherPackageComposerType? The whole composer?
If so... mark your composer with:

[DisableOtherPackage]

With:

public class DisableOtherComposerAttribute : DisableAttribute
{
    public DisableOtherComposerAttribute ()
    {
        var field = typeof(DisableAttribute).GetField("<DisabledType>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance);
        if (field == null) throw new Exception("panic: cannot get DisableAttribute.DisableType backing field.");

        var type = Type.GetType("Namespace.Of.OtherComposer,OtherComposerAssembly", false);
        field.SetValue(this, type);
    }
}

Replace "Namespace.Of.OtherComposer,OtherComposerAssembly" with the full name of the composer type you want to disable.

@zpqrtbnk
Copy link

Should you want to only remove the component but not disable the entire composer... you would need to iterate over the types in composition.Components() (not sure that can be done) and find the one you want to remove, by comparing names.

@Shazwazza
Copy link

Interesting... but note that Type.GetType will only find types already loaded by the AppDomain. If this works then great, but if it doesn't work, then you'll need to do Assembly.Load("OtherComposerAssembly").GetType("Namespace.Of.OtherComposer")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment