title: Member is declared in another module and needs to be imported
body: <p>I use Mono.Cecil to create a new custom attribute type and then add it to an existing type.</p>
  
  <p>To demonstrate it, I have a pre-existing DLL called "Sample" with type that is called "SampleType". </p>
  
  <p>I want to use Mono.Cecil to weave in a new type in "Sample" called "NewAttribute" and then add this attribute to "SampleType".</p>
  
  <p>The code looks like this: (not exactly but its good enough for example)</p>
  
  <pre><code>static void AddCustomeAttribute()
  {
      var module = ModuleDefinition.ReadModule(AssemblyName);
      var attrType = NewAttributeProvider.Add(module);
      var ctor = attrType.GetConstructors().First();
      //module.Import(ctor);
      CustomAttribute attribute = new CustomAttribute(ctor);
      attribute.ConstructorArguments.Add(new CustomAttributeArgument(module.TypeSystem.String, "InternalClass"));
      module.CustomAttributes.Add(attribute);
      module.Write(AssemblyName); //error
  }
  </code></pre>
  
  <p>-</p>
  
  <pre><code>public static TypeDefinition Add(ModuleDefinition targetModule)
  {
      var type = targetModule.AddType("Namespace", "NewAttribute", TypeAttributes.Public | TypeAttributes.Class, targetModule.Import(typeof(Attribute)));
      var stringType = targetModule.TypeSystem.String;
      var nameField = type.AddField(stringType, "_name");
      var nameProp = type.AddSimpleProperty(stringType, "Name", nameField);
  
      // generate a constructor body
      var constructor = type.AddConstructor(MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName, targetModule.TypeSystem.Void, new[] { stringType });
      constructor.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
      constructor.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_1));
      constructor.Body.Instructions.Add(Instruction.Create(OpCodes.Stfld, nameField));
      constructor.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
  
      var attrUsageType = targetModule.Import(typeof(AttributeUsageAttribute)).Resolve();
      //var att = targetModule.Import(typeof(AttributeUsageAttribute));
      //targetModule.Import(attrUsageType);
      var attributeTargetsType = targetModule.Import(typeof(AttributeTargets));
      //targetModule.Import(attributeTargetsType);
      var propertiesToSet = new Dictionary&lt;string, Tuple&lt;TypeReference, object&gt;&gt;
      {
          {"AllowMultiple", Tuple.Create(targetModule.TypeSystem.Boolean, (object)true)}
      };
      var usageAttr = type.AddCustomAttribute(attrUsageType, new[] { attributeTargetsType }, propertiesToSet);
      //targetModule.Import(usageAttr.AttributeType);
      targetModule.Types.Add(type);
      return type;
  }
  </code></pre>
  
  <p>-</p>
  
  <pre><code>public static CustomAttribute AddCustomAttribute(this TypeDefinition type, TypeDefinition attrType, TypeReference[] ctorParameters, Dictionary&lt;string, Tuple&lt;TypeReference, object&gt;&gt; propetiesToSet)
  {
      var attrUsageCtor = attrType.GetConstructors().Single(ctor =&gt; ctor.Parameters.Count == ctorParameters.Length &amp;&amp; ValidateParameters(ctor.Parameters, ctorParameters));
      type.Module.Import(attrUsageCtor);
      Collection&lt;CustomAttributeNamedArgument&gt; properties = new Collection&lt;CustomAttributeNamedArgument&gt;();
      foreach (KeyValuePair&lt;string, Tuple&lt;TypeReference, object&gt;&gt; typeReference in propetiesToSet)
      {
          properties.Add(new CustomAttributeNamedArgument(typeReference.Key, new CustomAttributeArgument(typeReference.Value.Item1, typeReference.Value.Item2)));
      }
      var customeAttr = new CustomAttribute(attrUsageCtor);
      foreach (var property in properties)
      {
          customeAttr.Properties.Add(property);
      }
      type.CustomAttributes.Add(customeAttr);
      return customeAttr;
  }
  </code></pre>
  
  <p>As you see, the comments in the code are attempts that I did to fix the problem but without success.
  I'm sure I'm missing something but I don't know what..</p>