If you have
public class Object{}
public class MeleeWeapon : Object{}
public class ShootingWeapon : Object{}
you can do
Object[] objects=new Object[]{new Object(), new MeleeWeapon(), new ShootingWeapon()};
Edit:
While you can make an array of class types in c# you cannot use them as you want.
In C# class types can be stored as Type. For instance
Type[] types= { typeof(Object), typeof(MeleeWeapon), typeof(RangedWeapon)};
Unfortunately for you pseudocode, C# will not allow you to use these as you described. C# performs type checks at compile time so it does not allow variable class types. You cannot not use the code
inventoryObject[0].GetComponent<c>()
inventoryObject[0].GetComponent("c") as c
because C# could not tell if there is a class type mismatch at compile time
However you can do what you described in your pseudocode using case/switch or if/else statements. If you want to save different variables depending on what class type you encounter in the array you needed to run different code depending on class type anyways.
i.e.
ClassType[]classTypes=new ClassType[]{ClassType.Object,
ClassType.MeleeWeapon,
ClassType.ShootingWeapon};
foreach (ClassType classType in classTypes)
{
switch(classType){
case ClassType.Object;
Write(inventoryObject[0].GetComponent<Object>(), Writer writer);
break;
case ClassType.Object;
Write(inventoryObject[1].GetComponent<MeleeWeapon>(), Writer writer);
break;
case ClassType.Object;
Write(inventoryObject[2].GetComponent<ShootingWeapon>(), Writer writer);
break;
}
}
or
Type[] types=new Type>[]{ typeof(Object), typeof(MeleeWeapon), typeof(ShootingWeapon) };
foreach (Type type in types)
{
if(type==typeOf(Object)){
Write(inventoryObject[0].GetComponent<Object>(), Writer writer);
}
else if(type==typeOf(MeleeWeapon)){
Write(inventoryObject[0].GetComponent<MeleeWeapon>(), Writer writer);
}
else if(type==typeOf(ShootingWeapon)){
Write(inventoryObject[0].GetComponent<ShootingWeapon>(), Writer writer);
}
}