Giter Club home page Giter Club logo

tostring's Introduction

AppVeyor Chat on Gitter NuGet Status Patrons on Open Collective

Extensible tool for weaving .net assemblies

Manipulating the IL of an assembly as part of a build requires a significant amount of plumbing code. This plumbing code involves knowledge of both the MSBuild and Visual Studio APIs. Fody attempts to eliminate that plumbing code through an extensible add-in model.

This is the codebase of core Fody engine. For more information on the larger Fody project see https://github.com/Fody/Home.

See Milestones for release notes.

Already a Patron? skip past this section

Community backed

Fody requires significant effort to maintain. As such it relies on financial support to ensure its long term viability.

It is expected that all developers using Fody become a Patron on OpenCollective.

See Licensing/Patron FAQ for more information.

Gold Sponsors

Support this project by becoming a Gold Sponsor. A large company logo will be added here with a link to your website.

PostSharp

Silver Sponsors

Support this project by becoming a Silver Sponsor. A medium company logo will be added here with a link to your website.

G-Research Particular Software

Bronze Sponsors

Support this project by becoming a Bronze Sponsor. The company avatar will show up here with a link to your OpenCollective Profile.

Patrons and sponsors

Thanks to all the backers and sponsors! Support this project by becoming a patron.

Documentation and Further Learning

Contributors

This project exists thanks to all the people who contribute.

tostring's People

Contributors

dependabot-preview[bot] avatar dependabot-support avatar dependabot[bot] avatar gayak avatar ghuntley avatar mujiansu avatar rjasica avatar simoncropp avatar timmurphy avatar tom-englert avatar tpluscode avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

tostring's Issues

Automatically set ToString attribute?

Hi,
Can you add an assembly wide attribute so that I can automatically set the ToString attribute in every class already in that project? This would ease the maintenance a bit...
Thx
Michael

IgnoreDuringToString is not working.

IgnoreDuringToString is not working.

I wrote the following code.

[ToString]
public abstract class MyBase
{
    [IgnoreDuringToString]
    public int BaseId { get; set; }
}

[ToString]
public class MyClass1 : Base.MyBase
{
    public string Name { get; set; }
}

[ToString]
public class MyClass2 : Base.MyBase
{
    public string Name { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        var myClass1 = new MyClass1();
        myClass1.BaseId = 9999;
        Debug.WriteLine(myClass1.ToString());

        var myClass2 = new MyClass2();
        myClass2.BaseId = 6666;
        Debug.WriteLine(myClass2.ToString());
    }
}

NG

When I execute ToString on MyClass2, "BaseId" that sets "IgnoreDuringToString" is displayed.

FodyToStringTest.zip

1.9.0 dropped support for Framework 4.5. Purposely?

I have used Version 1.8.0 with C# projects using Framework version 4.5.
When upgrading to 1.9.0 I receive an error message, saying

"Could not install package 'ToString.Fody 1.9.0'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.5', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author."

Have you dropped the support of Framework 4.5 on purpose? If yes - why?

Parent class: Process is terminated due to StackOverflowException

Hi,

I found in the code that it cannot manage Reference to parent. For example below code throw exception:
"Process is terminated due to StackOverflowException". Is it possible to write M parent as reference (like number "1" - because it was instanced before) ?


[ToString]
    public class M
    {
        public string A { get; set; }
        public string B { get; set; }

        public int C;

        private int D = 3;

        public List<string> Hello { get; set; }


        public M Parent { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var m = new M
            {
                A = "3",
                B = "4",
                C = 5,
                Hello = new List<string>
                {
                    "A",
                    "B",
                    "C"
                }
            };

            m.Parent = m;
            Console.Write(m.ToString());
            Console.ReadKey();
        }
    }

Allow IgnoreDuringToStringAttribute on fields

Is there a reason not to allow the IgnoreDuringToStringAttribute on fields? I am trying to use this plugin with another Fody plugin, Fielder, that converts fields to properties, and it does not compile because of the [AttributeUsage(AttributeTargets.Property)] restriction imposed on the attribute.

If you will allow it, then I will gladly provide a PR.

(Thanks for all the work, just started using the library recently, and I really like it. Tomorrow I am presenting about it to my colleagues :))

Problem with generic base class

Hi

ToString throws during build when it encounters a generic type in an inherited property. For example:

[ToString]
public class ChildClass : Parent<int>
{
   // irrelevant
}

public class Parent<T>
{
   public T SomeProperty { get; set; }
} 

In ModuleWeaver there is a line:

var hasCollections = properties.Any(x => x.PropertyType.Resolve().IsCollection());

The problem is that x.PropertyType.Resolve() returns null, because x.PropertyType is not a real type but rather the T generic type definition.

Tom

[Error] Fody unhandled exception

I get a compiler error from Fody when I work with generics, which are split on several dlls.

@SimonCropp

2>    Fody: Fody (version 2.1.2.0) Executing
2>MSBUILD : error : Fody: An unhandled exception occurred:
2>MSBUILD : error : Exception:
2>MSBUILD : error : Object reference not set to an instance to an object.
2>MSBUILD : error : StackTrace:
2>MSBUILD : error :    bei Mono.Cecil.ImportGenericContext.TypeParameter(String type, Int32 position)
2>MSBUILD : error :    bei Mono.Cecil.MetadataImporter.ImportTypeSpecification(TypeReference type, ImportGenericContext context)
2>MSBUILD : error :    bei Mono.Cecil.MetadataImporter.ImportType(TypeReference type, ImportGenericContext context)
2>MSBUILD : error :    bei Mono.Cecil.MetadataImporter.ImportTypeSpecification(TypeReference type, ImportGenericContext context)
2>MSBUILD : error :    bei Mono.Cecil.MetadataImporter.ImportType(TypeReference type, ImportGenericContext context)
2>MSBUILD : error :    bei Mono.Cecil.MetadataImporter.ImportMethod(MethodReference method, ImportGenericContext context)
2>MSBUILD : error :    bei Mono.Cecil.MetadataImporter.ImportReference(MethodReference method, IGenericParameterProvider context)
2>MSBUILD : error :    bei Mono.Cecil.ModuleDefinition.ImportReference(MethodReference method, IGenericParameterProvider context)
2>MSBUILD : error :    bei ModuleWeaver.AddPropertyCode(MethodBody body, Int32 index, PropertyDefinition property, TypeDefinition targetType, Collection`1 variables) in C:\projects\tostring\Fody\ModuleWeaver.cs:Zeile 183.
2>MSBUILD : error :    bei ModuleWeaver.AddToString(TypeDefinition type) in C:\projects\tostring\Fody\ModuleWeaver.cs:Zeile 97.
2>MSBUILD : error :    bei ModuleWeaver.Execute() in C:\projects\tostring\Fody\ModuleWeaver.cs:Zeile 51.
2>MSBUILD : error :    bei lambda_method(Closure , Object )
2>MSBUILD : error :    bei InnerWeaver.ExecuteWeavers() in C:\Code\Fody\Fody\FodyIsolated\InnerWeaver.cs:Zeile 181.
2>MSBUILD : error :    bei InnerWeaver.Execute() in C:\Code\Fody\Fody\FodyIsolated\InnerWeaver.cs:Zeile 86.
2>MSBUILD : error : Source:
2>MSBUILD : error : Mono.Cecil
2>MSBUILD : error : TargetSite:
2>MSBUILD : error : Mono.Cecil.TypeReference TypeParameter(System.String, Int32)
2>MSBUILD : error : 
2>    Fody:   Finished Fody 21ms.

Unfortunately, I can not reconstruct the same error in the test project, but another error!

1>    Fody: Fody (version 2.1.2.0) Executing
1>MSBUILD : error : Fody: An unhandled exception occurred:
1>MSBUILD : error : Exception:
1>MSBUILD : error : Member 'System.Collections.Generic.ICollection`1' is declared in another module and needs to be imported
1>MSBUILD : error : StackTrace:
1>MSBUILD : error :    bei Mono.Cecil.MetadataBuilder.LookupToken(IMetadataTokenProvider provider)
1>MSBUILD : error :    bei Mono.Cecil.SignatureWriter.WriteTypeSignature(TypeReference type)
1>MSBUILD : error :    bei Mono.Cecil.SignatureWriter.WriteMethodSignature(IMethodSignature method)
1>MSBUILD : error :    bei Mono.Cecil.MetadataBuilder.GetMemberRefSignature(MemberReference member)
1>MSBUILD : error :    bei Mono.Cecil.MetadataBuilder.CreateMemberRefRow(MemberReference member)
1>MSBUILD : error :    bei Mono.Cecil.MetadataBuilder.GetMemberRefToken(MemberReference member)
1>MSBUILD : error :    bei Mono.Cecil.MetadataBuilder.LookupToken(IMetadataTokenProvider provider)
1>MSBUILD : error :    bei Mono.Cecil.Cil.CodeWriter.WriteOperand(Instruction instruction)
1>MSBUILD : error :    bei Mono.Cecil.Cil.CodeWriter.WriteInstructions()
1>MSBUILD : error :    bei Mono.Cecil.Cil.CodeWriter.WriteResolvedMethodBody(MethodDefinition method)
1>MSBUILD : error :    bei Mono.Cecil.Cil.CodeWriter.WriteMethodBody(MethodDefinition method)
1>MSBUILD : error :    bei Mono.Cecil.MetadataBuilder.AddMethod(MethodDefinition method)
1>MSBUILD : error :    bei Mono.Cecil.MetadataBuilder.AddMethods(TypeDefinition type)
1>MSBUILD : error :    bei Mono.Cecil.MetadataBuilder.AddType(TypeDefinition type)
1>MSBUILD : error :    bei Mono.Cecil.MetadataBuilder.AddTypes()
1>MSBUILD : error :    bei Mono.Cecil.MetadataBuilder.BuildTypes()
1>MSBUILD : error :    bei Mono.Cecil.MetadataBuilder.BuildModule()
1>MSBUILD : error :    bei Mono.Cecil.MetadataBuilder.BuildMetadata()
1>MSBUILD : error :    bei Mono.Cecil.ModuleWriter.<>c.<BuildMetadata>b__2_0(MetadataBuilder builder, MetadataReader _)
1>MSBUILD : error :    bei Mono.Cecil.ModuleDefinition.Read[TItem,TRet](TItem item, Func`3 read)
1>MSBUILD : error :    bei Mono.Cecil.ModuleWriter.BuildMetadata(ModuleDefinition module, MetadataBuilder metadata)
1>MSBUILD : error :    bei Mono.Cecil.ModuleWriter.Write(ModuleDefinition module, Disposable`1 stream, WriterParameters parameters)
1>MSBUILD : error :    bei Mono.Cecil.ModuleWriter.WriteModule(ModuleDefinition module, Disposable`1 stream, WriterParameters parameters)
1>MSBUILD : error :    bei Mono.Cecil.ModuleDefinition.Write(String fileName, WriterParameters parameters)
1>MSBUILD : error :    bei InnerWeaver.WriteModule() in C:\Code\Fody\Fody\FodyIsolated\ModuleWriter.cs:Zeile 18.
1>MSBUILD : error :    bei InnerWeaver.Execute() in C:\Code\Fody\Fody\FodyIsolated\InnerWeaver.cs:Zeile 89.
1>MSBUILD : error : Source:
1>MSBUILD : error : Mono.Cecil
1>MSBUILD : error : TargetSite:
1>MSBUILD : error : Mono.Cecil.MetadataToken LookupToken(Mono.Cecil.IMetadataTokenProvider)
1>MSBUILD : error : 
1>    Fody:   Finished Fody 33ms.

FodyToStringError.zip

Generic type in referenced assembly

Hi

Having fixed the last two issues now comes a problem, which is a mixture thereof :).

In PropertyDefinitionExtensions there is a loop

while (propertyType != parent.Resolve())
{
    parentReference = parent.BaseType;
    parent = parent.BaseType.Resolve();
}

However for some reason the loop condition fails when the propertyType is the type from the referenced Assembly.

I'm blindly trying to fix this and my current stage is that I tried to compare FullNames at which point I got FatalExecutionEngineError. I figured that I had to Resolve() and Import() the property type

DeclaringType = property.Module.Import(genericInstanceType.Resolve())

However this causes invalid IL, because CLR tries to load the type the wrong assembly. Guess I'm doing something wrong but I'm a MSIL newbie :)

Please pull from my branch: https://github.com/tpluscode/ToString/commits/parent_in_referenced_assembly

Thanks,
Tom

BadImageFormatException when use abstract properties

BadImageFormatException occurred in the following code:

    public abstract class Foo
    {
        public abstract int Value { get; }
    }

    [ToString]
    public class Bar : Foo
    {
        public override int Value => 1;
    }

Because weaved code calls abstract property.

  public class Bar : Foo
  {
    public override int Value => 1;

    [GeneratedCode("Fody.ToString", "1.10.1.0")]
    [DebuggerNonUserCode]
    public override string ToString()
        => string.Format(
                CultureInfo.InvariantCulture,
                "{{T: \"Bar\", Value: {0}, Value: {1}}}",
                this.Value,
                base.Value /* abstract! */);
  }

Add inheritable ToStringAttribute

What about Adding An inheritable ToStringAttribute

I Finished That by Overriding the default ToStringAttribute By that
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = true)]
public sealed class ToStringAttribute : Attribute
{
}
it work fine
but what about making an option for that.

Cannot process types with parent in another assembly

Hi

I have a problem I don't know how to solve.

If there is a class annotated with [ToString], which derives from a type from another Assembly, the Cecil fails saying

Member 'X' is declared in another module and needs to be imported

I have seen a type name in place of X or a string representation of a property getter method. In my project it was simply "System.Int64" but when I added a failing test case it says "System.Int64 ParentAssembly.Parent::get_InParent()". Please have a look at branch https://github.com/tpluscode/ToString/commits/parent_in_referenced_assembly.

Do you know of a way to fix this problem?

Thanks,
Tom

ToString.Fody weaver fails with generic type parameter

Description

When trying to build a class marked with [ToString] that has a generic type parameter, the ToString.Fody weaver is encountering an error that fails the build. It appears to be throwing a NullReferenceException from the ModuleWeaver.AddGenericParameterNames method.

Minimal Repro

( Please see the attached FodyToStringGeneric.zip file for a very simple example project. )

  1. Add the [ToString] attribute to a class with a generic type parameter
[ToString]
public class MyClass<T>
{
    
}
  1. Attempt to build/compile
dotnet build
  1. Observe the following build error
MSBUILD : error : Fody: An unhandled exception occurred: [/Project/FodyToStringGeneric.csproj]
MSBUILD : error : Exception: [/Project/FodyToStringGeneric.csproj]
MSBUILD : error : Failed to execute weaver /Users/agaluzzi/.nuget/packages/tostring.fody/1.11.0/build/../weaver/ToString.Fody.dll [/Project/FodyToStringGeneric.csproj]
MSBUILD : error : Type: [/Project/FodyToStringGeneric.csproj]
MSBUILD : error : System.Exception [/Project/FodyToStringGeneric.csproj]
MSBUILD : error : StackTrace: [/Project/FodyToStringGeneric.csproj]
MSBUILD : error :    at InnerWeaver.ExecuteWeavers() in C:\projects\fody\FodyIsolated\InnerWeaver.cs:line 222 [/Project/FodyToStringGeneric.csproj]
MSBUILD : error :    at InnerWeaver.Execute() in C:\projects\fody\FodyIsolated\InnerWeaver.cs:line 112 [/Project/FodyToStringGeneric.csproj]
MSBUILD : error : Source: [/Project/FodyToStringGeneric.csproj]
MSBUILD : error : FodyIsolated [/Project/FodyToStringGeneric.csproj]
MSBUILD : error : TargetSite: [/Project/FodyToStringGeneric.csproj]
MSBUILD : error : Void ExecuteWeavers() [/Project/FodyToStringGeneric.csproj]
MSBUILD : error : Object reference not set to an instance of an object. [/Project/FodyToStringGeneric.csproj]
MSBUILD : error : Type: [/Project/FodyToStringGeneric.csproj]
MSBUILD : error : System.NullReferenceException [/Project/FodyToStringGeneric.csproj]
MSBUILD : error : StackTrace: [/Project/FodyToStringGeneric.csproj]
MSBUILD : error :    at ModuleWeaver.AddGenericParameterNames(TypeDefinition type, Collection`1 ins) [/Project/FodyToStringGeneric.csproj]
MSBUILD : error :    at ModuleWeaver.AddToString(TypeDefinition type) [/Project/FodyToStringGeneric.csproj]
MSBUILD : error :    at ModuleWeaver.Execute() [/Project/FodyToStringGeneric.csproj]
MSBUILD : error :    at InnerWeaver.ExecuteWeavers() in C:\projects\fody\FodyIsolated\InnerWeaver.cs:line 186 [/Project/FodyToStringGeneric.csproj]
MSBUILD : error : Source: [/Project/FodyToStringGeneric.csproj]
MSBUILD : error : ToString.Fody [/Project/FodyToStringGeneric.csproj]
MSBUILD : error : TargetSite: [/Project/FodyToStringGeneric.csproj]
MSBUILD : error : Void AddGenericParameterNames(Mono.Cecil.TypeDefinition, Mono.Collections.Generic.Collection`1[Mono.Cecil.Cil.Instruction]) [/Project/FodyToStringGeneric.csproj]
MSBUILD : error :  [/Project/FodyToStringGeneric.csproj]

Environment

ToString.Fody version 1.11.0
Fody version 6.6.3
.NET version 6.0.401
MSBuild version 17.3.1

Please Note

I did clone the ToString.Fody repo and tried to reproduce the problem with a unit test so that I could debug it, but had no success.

PCL Profile 259 not supported?

Hi,
I tried to install the ToString() component on a PCL targeting profile 259, but I got the error message below...

Could not install package 'ToString.Fody 1.7.3'. You are trying to install this package into a project that targets '.NETPortable,Version=v4.5,Profile=Profile259', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.

Marco.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.