如何 return 具有 API 中的功能的对象?

How to return an object with its functions from API?

考虑在 .NET 中使用 C# class

public class Person
{
    public string name { get; set; }
    public string surname { get; set; }
    
    public string Fullname()
    {
        return this.name + " " + this.surname;
    }
}

和端点

[HttpGet("getperson/")]
public ActionResult<Person> getPerson()
{
    ...
    return person;
}

在我的 Angular 应用程序中,我有函数

getPerson(){
    this.http.get(this.api + "getPerson").subscribe(person => {
        person.fullname() // is it possible to do this if the function is not implemented in the frontend?
    }
}

我能否以某种方式将函数与对象一起传递以使用它而不在前端实现它?

不,你不能。

将其视为必须编译的 TypeScript 代码。如果您从外部接收代码,该代码将不会被编译。

不可能。您需要在 Typescript 模型中编写 FullName 方法。

class Person
{
    name: string;
    surname: string;
    
    constructor(name: string, surname: string){
        this.name = name;
        this.surname = surname;
    }

    fullName(): string {
        return this.name + " " + this.surname; 
    }
}